About the code

Experiment: FSAReward (Ivan Grahek, Antonio Schettino, Gilles Pourtois, Ernst Koster, & Søren Andersen) (*: co-first authors) Code written by: Ivan Grahek (2019) Description: Summary of the behavioral and eeg (two types of normalization) single trial analyses.

Import packages etc.

# clear the environment
rm(list=ls()) 
# clear the console
cat("\014") 

#load packages and install them if they're not installed
if (!require("pacman")) install.packages("pacman")
pacman::p_load(plyr,Rmisc,yarrr,BayesFactor,reshape2,brms, broom, tidyverse, brmstools, BEST, knitr, here,psych)
# set seed
set.seed(42) 
#set.seed(32) 

# Set working directory
setwd(here())

Behavior

Import data

# Clear environemnt and import data------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# clear the environment
rm(list=ls()) 
# clear the console
cat("\014") 

#load packages and install them if they're not installed
if (!require("pacman")) install.packages("pacman")
pacman::p_load(plyr,Rmisc,yarrr,BayesFactor,reshape2,brms, broom, tidyverse, brmstools, BEST, knitr, here,psych)
# set seed
set.seed(42) 
# set directory
setwd(here())
# import data
data.raw = read.csv(file = here("data","Data_behavior_exp1_48pps.csv"),header=TRUE,na.strings="NaN")

# Prepare the dataset------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Adding and renaming variables 
# rename EventType variable
names(data.raw)[names(data.raw) == "EventType"] = "MovedDots" 
# add a variable with the name of the attended color instead of a numbers
data.raw$AttendedColor = ifelse(data.raw$AttendedColor==1,"red","blue")
# add a variable saying which color was linked with High_Rew (even numbers - blue was High_Rew)
data.raw$RewardedColor = ifelse(data.raw$ParticipantNo%%2==0,"blue","red") 
# add a variable with the name of the moved color instead of a numbers
data.raw$MovedDots = ifelse(data.raw$MovedDots==1,"red","blue") 
# split experimental phases into 6 isntead of 3 phases (trial 0-200: Bsln; trial 201-400: Acq; trial 401-600: Ext)
#data.raw$ExpPhase = cut(data.raw$Trial,breaks=c(0,100,200,300,400,500,600),labels=c("Bsln1","Bsln2","Acq1","Acq2","Ext1","Ext2")) 
# split experimental phases into 3 phases (trial 0-200: Bsln; trial 201-400: Acq; trial 401-600: Ext)
data.raw$ExpPhase = cut(data.raw$Trial,breaks=c(0,200,400,600),labels=c("Bsln","Acq","Ext")) # trial 0-200: Bsln; trial 201-400: Acq; trial 401-600: Ext

### Convert variables to be used in analyses into factors
data.raw[c("ParticipantNo", "AttendedColor","RewardedColor", "MovedDots", "ExpPhase" )] = 
  lapply(data.raw[c("ParticipantNo", "AttendedColor","RewardedColor", "MovedDots", "ExpPhase" )], factor)

### Create variables needed for the accuracy analyses
# count hits, false alarms, misses, correct rejections, and RT separately for each participant (their calculation is done in Matlab: see DataProcessing.m)
data.final = ddply(data.raw,.(ParticipantNo,ExpPhase,AttendedColor,RewardedColor,MovedDots),summarize,
                  numtrials=length(which(Response!=99)), # number of trials per condition (anything that is not 99 or any other number that we're not using)
                  Hits=length(which(Response==1)), # hits: attended color moved, correct response
                  FAs=length(which(Response==2)), # false alarms: attended color did not move, (wrong) response
                  Misses=length(which(Response==0)), # misses: attended color moved, no response
                  CRs=length(which(Response==3)), # correct rejections: attended color did not move, no response
                  mean.RT=mean(RT,na.rm=TRUE)) # mean RT per condition


################################################################## Calculate accuracy and RTs per condition ###############################################################################################################################################################################################################

# Prepare the data------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Calculate Hits and False alarms
# Hits are calculated for each participant in each condition on trials when they are attending the color that moved. 
# False alarms are  calculated for each participant in each condition on trials when they are attending the color that didn't move (the unattended color moved, but they responded)  
# Here we create the same number of hits & fas for each of the two conditions (moved attended or not)
data.final = ddply(data.final, .(ParticipantNo,ExpPhase,AttendedColor), transform, 
                 Hits = Hits[MovedDots==AttendedColor],
                 FAs = FAs[MovedDots!=AttendedColor])

# Keep only trials on which the attended color moved (we can do behavioral analysis only on those)
data.final = subset(data.final,MovedDots==AttendedColor)

### Calculate d'
# use loglinear transformation: add 0.5 to Hits, FAs, Misses, and CRs (Hautus, 1995, Behavior Research Methods, Instruments, & Computers),
# which is preferred over the 1/2N rule (Macmillan & Kaplan, 1985, Psychological Bulletin) because it results in less biased estimates of d'.
data.final =  ddply(data.final,.(ParticipantNo,ExpPhase,RewardedColor,AttendedColor,numtrials),summarise,
                    tot.Hits=Hits+.5, # hits
                    tot.FAs=FAs+.5, # false alarms
                    tot.Misses=(numtrials-Hits)+.5, # misses
                    tot.CRs=(numtrials-FAs)+.5, # correct rejections
                    Hit.Rate=tot.Hits/(tot.Hits+tot.Misses), # hit rate
                    FA.Rate=tot.FAs/(tot.FAs+tot.CRs), # false alarm rate
                    #dprime=qnorm(Hit.Rate)-qnorm(FA.Rate),
                    Hits.RTs=mean(mean.RT,na.rm=TRUE)) # mean RTs

# Calculate SDT indices with psycho
indices = psycho::dprime(data.final$tot.Hits, data.final$tot.FAs, data.final$tot.Misses, data.final$tot.CRs) 

data.final = cbind(data.final, indices) 
                      

### Create a final dataframe for accuracy and RTs analyses
# add a new variable specifying whether the participant is attending the high or Low_Rewed color
data.final$Condition = ifelse(data.final$RewardedColor==data.final$AttendedColor,"High_Rew","Low_Rew")
# make this variable a factor for further analyses
data.final$Condition = factor(data.final$Condition)

Hits vs. False alarms

Means - raw data

summary = ddply(data.final,.(ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Hit.Rate, na.rm = TRUE), digits = 2), " [", round(hdi(Hit.Rate)[[1]], digits = 2), " ", round(hdi(Hit.Rate)[[2]], digits = 2), "]")))

names(summary) = c("Reward phase", "Reward probability", "Hit Rate")

summary$`Reward phase` = recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)


summary.fa = ddply(data.final,.(ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(FA.Rate, na.rm = TRUE), digits = 2), " [", round(hdi(FA.Rate)[[1]], digits = 2), " ", round(hdi(FA.Rate)[[2]], digits = 2), "]")))

names(summary.fa) = c("Reward phase", "Reward probability", "FA Rate")

summary.fa = as.data.frame(summary.fa)

summary = cbind(summary,summary.fa$`FA Rate`)

names(summary) = c("Reward phase", "Reward probability", "Hit Rate","FA Rate")

kable(summary, caption = "Hit rates and false alarams per condition")
Hit rates and false alarams per condition
Reward phase Reward probability Hit Rate FA Rate
Baseline High 0.59 [ 0.32 0.7 ] 0.11 [ 0.01 0.52 ]
Baseline Low 0.58 [ 0.32 0.7 ] 0.12 [ 0.01 0.37 ]
Acquisition High 0.61 [ 0.37 0.8 ] 0.11 [ 0.01 0.52 ]
Acquisition Low 0.62 [ 0.47 0.76 ] 0.11 [ 0.01 0.35 ]
Extinction High 0.61 [ 0.3 0.71 ] 0.12 [ 0.01 0.52 ]
Extinction Low 0.61 [ 0.39 0.78 ] 0.1 [ 0.01 0.25 ]

Plot

# # Plot Hit rates

# Prepare the dataset
data.plot = data.final

# rename variables
colnames(data.plot)[colnames(data.plot)=="ExpPhase"] <- "Reward phase"
colnames(data.plot)[colnames(data.plot)=="Condition"] <- "Reward probability"

# rename conditions
data.plot$`Reward phase` = recode(data.plot$`Reward phase`,
                                "Acq" = "Acquisition",
                                "Bsln" = "Baseline",
                                "Ext" = "Extinction")

data.plot$`Reward probability` = recode(data.plot$`Reward probability`,
                                      "High_Rew" = "High",
                                      "Low_Rew" = "Low")
  # Pirate plot
  pirateplot(formula=Hit.Rate ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=data.plot, # data frame
             main="Hit rates", # main title
             ylim=c(0.1,0.9), # y-axis: limits
             ylab="Hit Rate", # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color

  # Pirate plot
  pirateplot(formula=FA.Rate ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=data.plot, # data frame
             main="FA rates", # main title
             ylim=c(-0.1,0.7), # y-axis: limits
             ylab="FA Rate", # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color

Statistics

# Set the working directory 
setwd(here("brms_models"))
# Import the model
model.full.Acc = readRDS("model.full.Acc.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.Acc, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# PP check
pp_check(model.full.Acc)

Plotting the best model

Sample from the posterior

# Analyzing the posterior and differences between conditions

post = posterior_samples(model.full.Acc, "^b")


################################################ Baseline ####

######### High reward
Baseline_High = post[["b_Intercept"]]
######### Low reward
Baseline_Low = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

################################################ Acquistion

######### High reward
Acquisition_High = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:ConditionLow_Rew"]]

################################################ Extinction

######### High reward
Extinction_High = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:ConditionLow_Rew"]]

Plot

# make a data frame
posterior_conditions = melt(data.frame(Baseline_High, Baseline_Low, Acquisition_High, Acquisition_Low, Extinction_High, Extinction_Low))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability"), "_", extra = "merge")

names(posterior_conditions)[3] = "Hit rate"


# Pirate plot
pirateplot(formula = `Hit rate` ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=posterior_conditions, # data frame
             main="Probability of a hit vs. a false alarm", # main title
             ylim=c(0.5, 4.5), # y-axis: limits
             ylab="Hit vs. false alarm", # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color

Table of means across conditions

library(HDInterval)

# Make a table with conditions
posterior_means = as.data.frame(c("Baseline High Reward", 
                                  "Baseline Low Reward", 
                                  "Acquisition High Reward", 
                                  "Acquisition Low Reward", 
                                  "Extinction High Reward", 
                                  "Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High), digits = 2), " [", round(hdi(Baseline_High)[[1]], digits = 2), " ", round(hdi(Baseline_High)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low), digits = 2), " [", round(hdi(Baseline_Low)[[1]], digits = 2), " ", round(hdi(Baseline_Low)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High), digits = 2), " [", round(hdi(Acquisition_High)[[1]], digits = 2), " ", round(hdi(Acquisition_High)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low), digits = 2), " [", round(hdi(Acquisition_Low)[[1]], digits = 2), " ", round(hdi(Acquisition_Low)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High), digits = 2), " [", round(hdi(Extinction_High)[[1]], digits = 2), " ", round(hdi(Extinction_High)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low), digits = 2), " [", round(hdi(Extinction_Low)[[1]], digits = 2), " ", round(hdi(Extinction_Low)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Reward Phase", "Reward Probability"), " ", extra = "merge")
                        
kable(posterior_means, caption = "Means per condition")
Means per condition
Reward Phase Reward Probability Mean [HDI]
Baseline High Reward 2.27 [ 1.84 2.7 ]
Baseline Low Reward 1.93 [ 1.57 2.31 ]
Acquisition High Reward 2.34 [ 1.91 2.8 ]
Acquisition Low Reward 2.11 [ 1.73 2.51 ]
Extinction High Reward 2.24 [ 1.82 2.69 ]
Extinction Low Reward 2.13 [ 1.75 2.5 ]

Inference about the best model

Check the difference between high reward in baseline vs. acquisition

Diff_Bsln_Acq_High = Acquisition_High - Baseline_High
plotPost(Diff_Bsln_Acq_High, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Check the difference between low reward in baseline vs. acquisition

Diff_Bsln_Acq_Low = Acquisition_Low - Baseline_Low
plotPost(Diff_Bsln_Acq_Low, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Check the difference between high and low reward in baseline vs. acquisition

Diff_Bsln_Acq_High_vs_Low = Diff_Bsln_Acq_Low - Diff_Bsln_Acq_High 
plotPost(Diff_Bsln_Acq_High_vs_Low, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low)[[2]], digits = 2), "]")
## [1] "Mean =  0.12  [ -0.21   0.46 ]"

Check the difference between high reward in acquisition vs. extinction

Diff_Acq_Ext_High = Extinction_High - Acquisition_High
plotPost(Diff_Acq_Ext_High, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Check the difference between low reward in acquisition vs. extinction

Diff_Acq_Ext_Low = Extinction_Low - Acquisition_Low
plotPost(Diff_Acq_Ext_Low, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Reaction times

Means - raw data

summary = ddply(data.final,.(ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Hits.RTs, na.rm = TRUE), digits = 2), " [", round(hdi(Hits.RTs)[[1]], digits = 2), " ", round(hdi(Hits.RTs)[[2]], digits = 2), "]")))

names(summary) = c("Reward phase", "Reward probability", "Reaction times")

summary$`Reward phase` = recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

kable(summary, caption = "Reaction times per condition")
Reaction times per condition
Reward phase Reward probability Reaction times
Baseline High 547.18 [ 460.9 612.74 ]
Baseline Low 552.93 [ 470.68 631.36 ]
Acquisition High 526 [ 457.23 599.49 ]
Acquisition Low 538.41 [ 465.32 605.14 ]
Extinction High 528.21 [ 448.5 599.83 ]
Extinction Low 538.21 [ 464.21 642.55 ]

Plot

# Prepare the dataset
data.plot = data.final

# rename variables
colnames(data.plot)[colnames(data.plot)=="ExpPhase"] = "Reward phase"
colnames(data.plot)[colnames(data.plot)=="Condition"] = "Reward probability"

# rename conditions
data.plot$`Reward phase` = recode(data.plot$`Reward phase`,
                                "Acq" = "Acquisition",
                                "Bsln" = "Baseline",
                                "Ext" = "Extinction")

data.plot$`Reward probability` = recode(data.plot$`Reward probability`,
                                      "High_Rew" = "High",
                                      "Low_Rew" = "Low")

# Pirate plot
  pirateplot(formula = Hits.RTs ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=data.plot, # data frame
             main="Reaction times", # main title
             ylim=c(400,700), # y-axis: limits
             ylab="Reaction time", # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color

Statistics

# Set the working directory 
setwd(here("brms_models"))

model.full.RT = readRDS("model.full.RT.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.RT, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.RT)

Plotting the best model

Sample from the posterior

# Analyzing the posterior and differences between conditions

post = posterior_samples(model.full.RT, "^b")


################################################ Baseline ####

######### High reward
Baseline_High = post[["b_Intercept"]]
######### Low reward
Baseline_Low = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

################################################ Acquistion

######### High reward
Acquisition_High = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:ConditionLow_Rew"]]

################################################ Extinction

######### High reward
Extinction_High = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:ConditionLow_Rew"]]
# make a data frame
posterior_conditions = melt(data.frame(Baseline_High, Baseline_Low, Acquisition_High, Acquisition_Low, Extinction_High, Extinction_Low))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability"), "_", extra = "merge")

names(posterior_conditions)[3] = "Reaction time"



# Pirate plot
pirateplot(formula = `Reaction time` ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
            data=posterior_conditions, # data frame
             main="Reaction times", # main title
             ylim=c(485,600), # y-axis: limits
             ylab="Reaction time", # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color

Table of means across conditions

library(HDInterval)

# Make a table with conditions
posterior_means = as.data.frame(c("Baseline High Reward", 
                                  "Baseline Low Reward", 
                                  "Acquisition High Reward", 
                                  "Acquisition Low Reward", 
                                  "Extinction High Reward", 
                                  "Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High), digits = 2), " [", round(hdi(Baseline_High)[[1]], digits = 2), " ", round(hdi(Baseline_High)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low), digits = 2), " [", round(hdi(Baseline_Low)[[1]], digits = 2), " ", round(hdi(Baseline_Low)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High), digits = 2), " [", round(hdi(Acquisition_High)[[1]], digits = 2), " ", round(hdi(Acquisition_High)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low), digits = 2), " [", round(hdi(Acquisition_Low)[[1]], digits = 2), " ", round(hdi(Acquisition_Low)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High), digits = 2), " [", round(hdi(Extinction_High)[[1]], digits = 2), " ", round(hdi(Extinction_High)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low), digits = 2), " [", round(hdi(Extinction_Low)[[1]], digits = 2), " ", round(hdi(Extinction_Low)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Reward Phase Reward Probability Mean [HDI]
Baseline High Reward 543.55 [ 531.17 555.46 ]
Baseline Low Reward 547.42 [ 534.65 559.94 ]
Acquisition High Reward 529.98 [ 518.51 541.72 ]
Acquisition Low Reward 539.14 [ 527.41 551.09 ]
Extinction High Reward 527.04 [ 514.29 539.83 ]
Extinction Low Reward 533.07 [ 519.99 546.75 ]

Inference about the best model

Check the difference between high reward in baseline vs. acquisition

Diff_Bsln_Acq_High = Acquisition_High - Baseline_High
plotPost(Diff_Bsln_Acq_High, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Check the difference between low reward in baseline vs. acquisition

Diff_Bsln_Acq_Low = Acquisition_Low - Baseline_Low 
plotPost(Diff_Bsln_Acq_Low, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Check the difference between high and low reward in baseline vs. acquisition

Diff_Bsln_Acq_High_vs_Low = Diff_Bsln_Acq_High - Diff_Bsln_Acq_Low 
plotPost(Diff_Bsln_Acq_High_vs_Low, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low)[[2]], digits = 2), "]")
## [1] "Mean =  -5.29  [ -13.83   2.83 ]"

Check the difference between high reward in acquisition vs. extinction

Diff_Acq_Ext_High = Extinction_High - Acquisition_High
plotPost(Diff_Acq_Ext_High, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

Check the difference between low reward in acquisition vs. extinction

Diff_Acq_Ext_Low = Extinction_Low - Acquisition_Low
plotPost(Diff_Acq_Ext_Low, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

EEG - baseline normalization

Topography & spectra

Topography and spectra

Topography and spectra

All trials

Prepare the dataset

# import data
data.raw = read.csv(file = here("data","singleTrial_amplitudes_movement_and_nomovement.csv"),header=TRUE,na.strings="NaN") 

data = data.raw

# Clean the subject name variable
data$participant = gsub('VP', '', data$participant)
data$participant = as.numeric(data$participant)

# Change the names of the variables
names(data)[names(data) == "participant"] = "Subject"
names(data)[names(data) == "amplitude"] = "Amplitude"
names(data)[names(data) == "frequency"] = "Frequency"


# Add new variables based on the condition
data$ExpPhase[data$condition == 1 | data$condition == 2 | data$condition == 11 | data$condition == 12]="Bsln"
data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

data$AttendedColor[data$condition == 1 | data$condition == 3 | data$condition == 5 | data$condition == 11 |data$condition == 13 | data$condition == 15]="Red"
data$AttendedColor[data$condition == 2 | data$condition == 4 | data$condition == 6 | data$condition == 12 |data$condition == 14 | data$condition == 16]="Blue"

data$Movement[data$condition == 1 | data$condition == 2 | data$condition == 3 | data$condition == 4 |data$condition == 5 | data$condition == 6]="NoMovement"
data$Movement[data$condition == 11 | data$condition == 12 | data$condition == 13 | data$condition == 14 |data$condition == 15 | data$condition == 16]="Movement"

data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

# Add the variable defining which color is rewarded based on the participant number
data$RewardedColor = ifelse(data$Subject%%2==0,"Blue","Red") # if participant number is even, blue was rewarded

# Switch the Frequency to the color
data$RecordedFrequency = ifelse(data$Frequency==10,"Blue","Red") # if the recorded frequency is 10Hz assign Blue (color flickering at 10Hz), otherwise assign Red (color flickering at 12Hz)

# Make a new condition based on the attended color and the rewarded color
data$Condition = ifelse(data$AttendedColor==data$RewardedColor, "High_Rew","Low_Rew")

# Make a new condition based on the attended color and the recorded frequency
data$Attention = ifelse(data$AttendedColor==data$RecordedFrequency, "Att","NotAtt")

# Make a new condition based the Condition and the Attention
data$RecordingAndCondition = with(data, paste0(Condition,"_",Attention))

# Select variables which we want to keep
data = subset(data, select=c("Subject","RewardedColor","ExpPhase","AttendedColor","Condition","RecordedFrequency","Attention","RecordingAndCondition","Amplitude","Movement"))

# Sort the data 
data = data[with(data, order(Subject)), ]

# Normalize the two frequencies
#Make a new variable with mean amplitude across all conditions for each participant and each frequency  !!! originally did not include ExpPhase below !!!
data = ddply(data,.(Subject,RecordedFrequency),transform,
                    MeanAmplitude = mean(Amplitude[ExpPhase=="Bsln"],na.rm=TRUE),
                    SDAmplitude =   sd(Amplitude,na.rm=TRUE))

# data = ddply(data,.(Subject,RecordedFrequency),transform,
#              MeanAmplitude = mean(Amplitude,na.rm=TRUE),
#              SDAmplitude =   sd(Amplitude,na.rm=TRUE))

#MeanAmplitude = mean(Amplitude[ExpPhase=="Baseline"],na.rm=TRUE),   [ExpPhase=="Bsln"]

# Divide amplitudes in each Subject, Frequency, and Condition by the Mean Amplitude
data$Amplitude = data$Amplitude/data$MeanAmplitude

# Calculate the attention indexes - Selectivity (attended-unattended) & total enhancement (attended+unattended) (Andersen & Muller, 2010, PNAS)
data.diff = ddply(data, .(Subject,ExpPhase,Condition), transform, Selectivity = Amplitude[Attention=="Att"]-Amplitude[Attention=="NotAtt"],TotalEnhancement=Amplitude[Attention=="Att"]+Amplitude[Attention=="NotAtt"])
# Delete the Attention column and rows which are not necessary (indexes repeated twice)
data.diff = subset(data.diff,Attention=="Att") #keep only Att as it is equal to NotAtt
data.diff$Attention = NULL  #drop the Attention column

# Sort the data 
data.diff$ExpPhase = factor(data.diff$ExpPhase, levels = c("Bsln","Acq","Ext"))
data.diff = data.diff[order(data.diff$Subject,data.diff$Condition,data.diff$ExpPhase),]

# # Calculate the reward index - High reward minus Low reward
# data.reward = ddply(data, .(Subject,ExpPhase,Attention), transform, Reward = Amplitude[Condition=="High_Rew"]-Amplitude[Condition=="Low_Rew"])
# # Delete the Attention column and rows which are not necessary (indexes repeated twice)
# data.reward = subset(data.reward,Condition=="High_Rew") #keep only Att as it is equal to NotAtt
# data.reward$Condition = NULL  #drop the Condition column
# 
# # Sort the data 
# data.reward$ExpPhase = factor(data.reward$ExpPhase, levels = c("Bsln","Acq","Ext"))
# data.reward = data.reward[order(data.reward$Subject,data.reward$Attention,data.reward$ExpPhase),]

Means - raw data

summary = ddply(data,.(Attention,ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Amplitude, na.rm = TRUE), digits = 2), " [", round(hdi(Amplitude)[[1]], digits = 2), " ", round(hdi(Amplitude)[[2]], digits = 2), "]")))

names(summary) = c("Attention", "Reward phase", "Reward probability", "Amplitude")

summary$Attention = dplyr::recode(summary$Attention,
                           "Att" = "Attended",
                           "NotAtt" = "Unattended")

summary$`Reward phase` = dplyr::recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = dplyr::recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

summary$`Reward phase` = factor(summary$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
summary = summary[order(summary$Attention,summary$`Reward phase`,summary$`Reward probability`),]
row.names(summary) = NULL

knitr::kable(summary, caption = "Amplitudes per condition")
Amplitudes per condition
Attention Reward phase Reward probability Amplitude
Attended Baseline High 1.08 [ 0.36 1.77 ]
Attended Baseline Low 1.07 [ 0.37 1.77 ]
Attended Acquisition High 1.08 [ 0.36 1.77 ]
Attended Acquisition Low 1.06 [ 0.37 1.78 ]
Attended Extinction High 1.1 [ 0.36 1.88 ]
Attended Extinction Low 1.09 [ 0.35 1.88 ]
Unattended Baseline High 0.93 [ 0.35 1.7 ]
Unattended Baseline Low 0.92 [ 0.32 1.62 ]
Unattended Acquisition High 0.93 [ 0.31 1.65 ]
Unattended Acquisition Low 0.91 [ 0.28 1.57 ]
Unattended Extinction High 0.95 [ 0.28 1.69 ]
Unattended Extinction Low 0.97 [ 0.26 1.69 ]

Plots - raw data

# Plot amplitude across experiment phases

# prepare data for plotting
dataPlot = data

dataPlot =  ddply(dataPlot,.(Subject,ExpPhase,Condition,Attention),summarise,
                    Amplitude=mean(Amplitude,na.rm=TRUE)) # mean RTs


# rename variables
colnames(dataPlot)[colnames(dataPlot)=="ExpPhase"] <- "Reward phase"
colnames(dataPlot)[colnames(dataPlot)=="Condition"] <- "Reward probability"

# rename conditions
dataPlot$`Reward phase` = dplyr::recode(dataPlot$`Reward phase`,
                                  "Acq" = "Acquisition",
                                  "Bsln" = "Baseline",
                                  "Ext" = "Extinction")

dataPlot$`Reward probability` = dplyr::recode(dataPlot$`Reward probability`,
                                        "High_Rew" = "High",
                                        "Low_Rew" = "Low")

#order
dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(dataPlot,Attention=="Att")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(dataPlot,Attention=="NotAtt")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.5,1.7), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color
}

Statistics

# Set the working directory in order to load the models
setwd(here("brms_models"))
model.full.threefactors = readRDS("full.EEG.allsubs.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.threefactors, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.threefactors)

Plotting the best model

post = posterior_samples(model.full.threefactors, "^b")

# Calculate posteriors for each condition

################################################ Baseline ####

##################### Attended

######### High reward
Baseline_High_Attended = post[["b_Intercept"]]
######### Low reward
Baseline_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

##################### Not Attended

######### High reward
Baseline_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]]
######### Low reward
Baseline_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:AttentionNotAtt"]]

################################################ Acquistion

##################### Attended

######### High reward
Acquisition_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]]

##################### Not Attended

######### High reward
Acquisition_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseAcq:AttentionNotAtt"]]
  
######### Low reward
Acquisition_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq:AttentionNotAtt"]]

################################################ Extinction

##################### Attended

######### High reward
Extinction_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt"]]

##################### Not Attended

######### High reward
Extinction_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseExt:AttentionNotAtt"]]
######### Low reward
Extinction_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt:AttentionNotAtt"]]
# make a data frame

posterior_conditions = melt(data.frame(Baseline_High_Attended, Baseline_High_NotAttended, Baseline_Low_Attended, Baseline_Low_NotAttended, Acquisition_High_Attended, Acquisition_High_NotAttended, Acquisition_Low_Attended, Acquisition_Low_NotAttended, Extinction_High_Attended, Extinction_High_NotAttended, Extinction_Low_Attended, Extinction_Low_NotAttended))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability", "Attention"), "_", extra = "merge")

posterior_conditions$Attention = recode(posterior_conditions$Attention,
                           "Attended" = "Attended",
                           "NotAttended" = "Unattended")

names(posterior_conditions)[4] = "Amplitude"


#order
#dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
#dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Attended")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Unattended")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.7,1.3), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color
}

Table of means across conditions

# Make a table with conditions
posterior_means = as.data.frame(c("Attended Baseline High Reward", 
                                  "Attended Baseline Low Reward", 
                                  "Attended Acquisition High Reward", 
                                  "Attended Acquisition Low Reward", 
                                  "Attended Extinction High Reward", 
                                  "Attended Extinction Low Reward",
                                  "Unattended Baseline High Reward", 
                                  "Unattended Baseline Low Reward", 
                                  "Unattended Acquisition High Reward", 
                                  "Unattended Acquisition Low Reward", 
                                  "Unattended Extinction High Reward", 
                                  "Unattended Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High_Attended), digits = 2), " [", round(hdi(Baseline_High_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_Attended), digits = 2), " [", round(hdi(Baseline_Low_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_Attended)[[2]], digits = 2), "]"),
                        
                        paste(round(mean(Acquisition_High_Attended), digits = 2), " [", round(hdi(Acquisition_High_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_Attended), digits = 2), " [", round(hdi(Acquisition_Low_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_Attended), digits = 2), " [", round(hdi(Extinction_High_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_Attended), digits = 2), " [", round(hdi(Extinction_Low_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_High_NotAttended), digits = 2), " [", round(hdi(Baseline_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_NotAttended), digits = 2), " [", round(hdi(Baseline_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High_NotAttended), digits = 2), " [", round(hdi(Acquisition_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_NotAttended), digits = 2), " [", round(hdi(Acquisition_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_NotAttended), digits = 2), " [", round(hdi(Extinction_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_NotAttended), digits = 2), " [", round(hdi(Extinction_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_NotAttended)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Attention", "Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Attention Reward Phase Reward Probability Mean [HDI]
Attended Baseline High Reward 1.08 [ 1.05 1.1 ]
Attended Baseline Low Reward 1.07 [ 1.04 1.09 ]
Attended Acquisition High Reward 1.08 [ 1.05 1.12 ]
Attended Acquisition Low Reward 1.06 [ 1.03 1.1 ]
Attended Extinction High Reward 1.1 [ 1.07 1.13 ]
Attended Extinction Low Reward 1.09 [ 1.06 1.13 ]
Unattended Baseline High Reward 0.93 [ 0.91 0.96 ]
Unattended Baseline Low Reward 0.92 [ 0.9 0.95 ]
Unattended Acquisition High Reward 0.93 [ 0.89 0.96 ]
Unattended Acquisition Low Reward 0.91 [ 0.87 0.95 ]
Unattended Extinction High Reward 0.95 [ 0.92 0.99 ]
Unattended Extinction Low Reward 0.97 [ 0.92 1.01 ]

Inference about the best model

Attended vs. unattended

Check the difference between attended and not attended in baseline high rewarded

Diff_Att_NotAtt_Bsln_High = Baseline_High_Attended - Baseline_High_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in baseline low rewarded

Diff_Att_NotAtt_Bsln_Low = Baseline_Low_Attended - Baseline_Low_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition high rewarded

Diff_Att_NotAtt_Acq_High = Acquisition_High_Attended - Acquisition_High_NotAttended
plotPost(Diff_Att_NotAtt_Acq_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition low rewarded

Diff_Att_NotAtt_Acq_Low = Acquisition_Low_Attended - Acquisition_Low_NotAttended
plotPost(Diff_Att_NotAtt_Acq_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction high rewarded

Diff_Att_NotAtt_Ext_High = Extinction_High_Attended - Extinction_High_NotAttended
plotPost(Diff_Att_NotAtt_Ext_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction low rewarded

Diff_Att_NotAtt_Ext_Low = Extinction_Low_Attended - Extinction_Low_NotAttended
plotPost(Diff_Att_NotAtt_Ext_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Comparison between phases

Check the difference between baseline and acquisition in high reward attended

Diff_Bsln_Acq_High_Att = Baseline_High_Attended - Acquisition_High_Attended
plotPost(Diff_Bsln_Acq_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward attended

Diff_Bsln_Acq_Low_Att = Baseline_Low_Attended - Acquisition_Low_Attended
plotPost(Diff_Bsln_Acq_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward not attended

Diff_Bsln_Acq_High_NotAtt = Baseline_High_NotAttended - Acquisition_High_NotAttended
plotPost(Diff_Bsln_Acq_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward not attended

Diff_Bsln_Acq_Low_NotAtt = Acquisition_Low_NotAttended - Baseline_Low_NotAttended  
plotPost(Diff_Bsln_Acq_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward attended

Diff_Acq_Ext_High_Att = Acquisition_High_Attended - Extinction_High_Attended
plotPost(Diff_Acq_Ext_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward attended

Diff_Acq_Ext_Low_Att = Extinction_Low_Attended - Acquisition_Low_Attended 
plotPost(Diff_Acq_Ext_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward not attended

Diff_Acq_Ext_High_NotAtt = Extinction_High_NotAttended - Acquisition_High_NotAttended 
plotPost(Diff_Acq_Ext_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward not attended

Diff_Acq_Ext_Low_NotAtt = Extinction_Low_NotAttended - Acquisition_Low_NotAttended 
plotPost(Diff_Acq_Ext_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Baseline difference

Check the difference between high and low reward in baseline attended

Diff_Bsln_High_Low_Att = Baseline_High_Attended - Baseline_Low_Attended
plotPost(Diff_Bsln_High_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between high and low reward in baseline not attended

Diff_Bsln_High_Low_NotAtt = Baseline_High_NotAttended - Baseline_Low_NotAttended
plotPost(Diff_Bsln_High_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward unattended vs. attended

Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended = Diff_Bsln_High_Low_NotAtt - Diff_Bsln_High_Low_Att 
plotPost(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[2]], digits = 2), "]")
## [1] "Mean =  0  [ -0.02   0.02 ]"

No movement trials

Prepare the dataset

# import data
data.raw = read.csv(file = here("data","singleTrial_amplitudes_movement_and_nomovement.csv"),header=TRUE,na.strings="NaN") 

data = data.raw

# Clean the subject name variable
data$participant = gsub('VP', '', data$participant)
data$participant = as.numeric(data$participant)

# Change the names of the variables
names(data)[names(data) == "participant"] = "Subject"
names(data)[names(data) == "amplitude"] = "Amplitude"
names(data)[names(data) == "frequency"] = "Frequency"


# Add new variables based on the condition
data$ExpPhase[data$condition == 1 | data$condition == 2 | data$condition == 11 | data$condition == 12]="Bsln"
data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

data$AttendedColor[data$condition == 1 | data$condition == 3 | data$condition == 5 | data$condition == 11 |data$condition == 13 | data$condition == 15]="Red"
data$AttendedColor[data$condition == 2 | data$condition == 4 | data$condition == 6 | data$condition == 12 |data$condition == 14 | data$condition == 16]="Blue"

data$Movement[data$condition == 1 | data$condition == 2 | data$condition == 3 | data$condition == 4 |data$condition == 5 | data$condition == 6]="NoMovement"
data$Movement[data$condition == 11 | data$condition == 12 | data$condition == 13 | data$condition == 14 |data$condition == 15 | data$condition == 16]="Movement"

data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

# Add the variable defining which color is rewarded based on the participant number
data$RewardedColor = ifelse(data$Subject%%2==0,"Blue","Red") # if participant number is even, blue was rewarded

# Switch the Frequency to the color
data$RecordedFrequency = ifelse(data$Frequency==10,"Blue","Red") # if the recorded frequency is 10Hz assign Blue (color flickering at 10Hz), otherwise assign Red (color flickering at 12Hz)

# Make a new condition based on the attended color and the rewarded color
data$Condition = ifelse(data$AttendedColor==data$RewardedColor, "High_Rew","Low_Rew")

# Make a new condition based on the attended color and the recorded frequency
data$Attention = ifelse(data$AttendedColor==data$RecordedFrequency, "Att","NotAtt")

# Make a new condition based the Condition and the Attention
data$RecordingAndCondition = with(data, paste0(Condition,"_",Attention))

# Select variables which we want to keep
data = subset(data, select=c("Subject","RewardedColor","ExpPhase","AttendedColor","Condition","RecordedFrequency","Attention","RecordingAndCondition","Amplitude","Movement"))

# Sort the data 
data = data[with(data, order(Subject)), ]

# Normalize the two frequencies
#Make a new variable with mean amplitude across all conditions for each participant and each frequency  !!! originally did not include ExpPhase below !!!
data = ddply(data,.(Subject,RecordedFrequency),transform,
                    MeanAmplitude = mean(Amplitude[ExpPhase=="Bsln"],na.rm=TRUE),
                    SDAmplitude =   sd(Amplitude,na.rm=TRUE))

# data = ddply(data,.(Subject,RecordedFrequency),transform,
#              MeanAmplitude = mean(Amplitude,na.rm=TRUE),
#              SDAmplitude =   sd(Amplitude,na.rm=TRUE))

#MeanAmplitude = mean(Amplitude[ExpPhase=="Baseline"],na.rm=TRUE),   [ExpPhase=="Bsln"]

# Divide amplitudes in each Subject, Frequency, and Condition by the Mean Amplitude
data$Amplitude = data$Amplitude/data$MeanAmplitude

# Calculate the attention indexes - Selectivity (attended-unattended) & total enhancement (attended+unattended) (Andersen & Muller, 2010, PNAS)
data.diff = ddply(data, .(Subject,ExpPhase,Condition), transform, Selectivity = Amplitude[Attention=="Att"]-Amplitude[Attention=="NotAtt"],TotalEnhancement=Amplitude[Attention=="Att"]+Amplitude[Attention=="NotAtt"])
# Delete the Attention column and rows which are not necessary (indexes repeated twice)
data.diff = subset(data.diff,Attention=="Att") #keep only Att as it is equal to NotAtt
data.diff$Attention = NULL  #drop the Attention column

# Sort the data 
data.diff$ExpPhase = factor(data.diff$ExpPhase, levels = c("Bsln","Acq","Ext"))
data.diff = data.diff[order(data.diff$Subject,data.diff$Condition,data.diff$ExpPhase),]

# Take only the no movement trials
data = subset(data,Movement=="NoMovement")

Means - raw data

summary = ddply(data,.(Attention,ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Amplitude, na.rm = TRUE), digits = 2), " [", round(hdi(Amplitude)[[1]], digits = 2), " ", round(hdi(Amplitude)[[2]], digits = 2), "]")))

names(summary) = c("Attention", "Reward phase", "Reward probability", "Amplitude")

summary$Attention = dplyr::recode(summary$Attention,
                           "Att" = "Attended",
                           "NotAtt" = "Unattended")

summary$`Reward phase` = dplyr::recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = dplyr::recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

summary$`Reward phase` = factor(summary$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
summary = summary[order(summary$Attention,summary$`Reward phase`,summary$`Reward probability`),]
row.names(summary) = NULL

knitr::kable(summary, caption = "Amplitudes per condition")
Amplitudes per condition
Attention Reward phase Reward probability Amplitude
Attended Baseline High 1.1 [ 0.37 1.81 ]
Attended Baseline Low 1.09 [ 0.37 1.83 ]
Attended Acquisition High 1.08 [ 0.37 1.75 ]
Attended Acquisition Low 1.08 [ 0.37 1.8 ]
Attended Extinction High 1.12 [ 0.32 1.87 ]
Attended Extinction Low 1.12 [ 0.33 1.9 ]
Unattended Baseline High 0.95 [ 0.33 1.7 ]
Unattended Baseline Low 0.93 [ 0.29 1.64 ]
Unattended Acquisition High 0.96 [ 0.28 1.66 ]
Unattended Acquisition Low 0.92 [ 0.28 1.59 ]
Unattended Extinction High 0.99 [ 0.28 1.79 ]
Unattended Extinction Low 0.99 [ 0.27 1.76 ]

Plots - raw data

# Plot amplitude across experiment phases

# prepare data for plotting
dataPlot = data

dataPlot =  ddply(dataPlot,.(Subject,ExpPhase,Condition,Attention),summarise,
                    Amplitude=mean(Amplitude,na.rm=TRUE)) # mean RTs


# rename variables
colnames(dataPlot)[colnames(dataPlot)=="ExpPhase"] <- "Reward phase"
colnames(dataPlot)[colnames(dataPlot)=="Condition"] <- "Reward probability"

# rename conditions
dataPlot$`Reward phase` = dplyr::recode(dataPlot$`Reward phase`,
                                  "Acq" = "Acquisition",
                                  "Bsln" = "Baseline",
                                  "Ext" = "Extinction")

dataPlot$`Reward probability` = dplyr::recode(dataPlot$`Reward probability`,
                                        "High_Rew" = "High",
                                        "Low_Rew" = "Low")

#order
dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(dataPlot,Attention=="Att")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(dataPlot,Attention=="NotAtt")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.5,1.7), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color
}

Statistics - no movement trials

# Set the working directory in order to load the models
setwd(here("brms_models"))
model.full.threefactors = readRDS("full.EEG.allsubs.nomovement.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.threefactors, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.threefactors)

Plotting the best model

post = posterior_samples(model.full.threefactors, "^b")

# Calculate posteriors for each condition

################################################ Baseline ####

##################### Attended

######### High reward
Baseline_High_Attended = post[["b_Intercept"]]
######### Low reward
Baseline_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

##################### Not Attended

######### High reward
Baseline_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]]
######### Low reward
Baseline_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:AttentionNotAtt"]]

################################################ Acquistion

##################### Attended

######### High reward
Acquisition_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]]

##################### Not Attended

######### High reward
Acquisition_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseAcq:AttentionNotAtt"]]
  
######### Low reward
Acquisition_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq:AttentionNotAtt"]]

################################################ Extinction

##################### Attended

######### High reward
Extinction_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt"]]

##################### Not Attended

######### High reward
Extinction_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseExt:AttentionNotAtt"]]
######### Low reward
Extinction_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt:AttentionNotAtt"]]
# make a data frame

posterior_conditions = melt(data.frame(Baseline_High_Attended, Baseline_High_NotAttended, Baseline_Low_Attended, Baseline_Low_NotAttended, Acquisition_High_Attended, Acquisition_High_NotAttended, Acquisition_Low_Attended, Acquisition_Low_NotAttended, Extinction_High_Attended, Extinction_High_NotAttended, Extinction_Low_Attended, Extinction_Low_NotAttended))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability", "Attention"), "_", extra = "merge")

posterior_conditions$Attention = recode(posterior_conditions$Attention,
                           "Attended" = "Attended",
                           "NotAttended" = "Unattended")

names(posterior_conditions)[4] = "Amplitude"


#order
#dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
#dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Attended")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Unattended")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.7,1.3), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color
}

Table of means across conditions

# Make a table with conditions
posterior_means = as.data.frame(c("Attended Baseline High Reward", 
                                  "Attended Baseline Low Reward", 
                                  "Attended Acquisition High Reward", 
                                  "Attended Acquisition Low Reward", 
                                  "Attended Extinction High Reward", 
                                  "Attended Extinction Low Reward",
                                  "Unattended Baseline High Reward", 
                                  "Unattended Baseline Low Reward", 
                                  "Unattended Acquisition High Reward", 
                                  "Unattended Acquisition Low Reward", 
                                  "Unattended Extinction High Reward", 
                                  "Unattended Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High_Attended), digits = 2), " [", round(hdi(Baseline_High_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_Attended), digits = 2), " [", round(hdi(Baseline_Low_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_Attended)[[2]], digits = 2), "]"),
                        
                        paste(round(mean(Acquisition_High_Attended), digits = 2), " [", round(hdi(Acquisition_High_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_Attended), digits = 2), " [", round(hdi(Acquisition_Low_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_Attended), digits = 2), " [", round(hdi(Extinction_High_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_Attended), digits = 2), " [", round(hdi(Extinction_Low_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_High_NotAttended), digits = 2), " [", round(hdi(Baseline_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_NotAttended), digits = 2), " [", round(hdi(Baseline_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High_NotAttended), digits = 2), " [", round(hdi(Acquisition_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_NotAttended), digits = 2), " [", round(hdi(Acquisition_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_NotAttended), digits = 2), " [", round(hdi(Extinction_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_NotAttended), digits = 2), " [", round(hdi(Extinction_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_NotAttended)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Attention", "Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Attention Reward Phase Reward Probability Mean [HDI]
Attended Baseline High Reward 1.1 [ 1.07 1.12 ]
Attended Baseline Low Reward 1.09 [ 1.06 1.12 ]
Attended Acquisition High Reward 1.08 [ 1.05 1.12 ]
Attended Acquisition Low Reward 1.08 [ 1.05 1.12 ]
Attended Extinction High Reward 1.12 [ 1.08 1.16 ]
Attended Extinction Low Reward 1.12 [ 1.08 1.17 ]
Unattended Baseline High Reward 0.95 [ 0.91 0.98 ]
Unattended Baseline Low Reward 0.93 [ 0.89 0.97 ]
Unattended Acquisition High Reward 0.96 [ 0.92 0.99 ]
Unattended Acquisition Low Reward 0.94 [ 0.89 0.99 ]
Unattended Extinction High Reward 0.99 [ 0.94 1.04 ]
Unattended Extinction Low Reward 1 [ 0.94 1.07 ]

Inference about the best model

Attended vs. unattended

Check the difference between attended and not attended in baseline high rewarded

Diff_Att_NotAtt_Bsln_High = Baseline_High_Attended - Baseline_High_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in baseline low rewarded

Diff_Att_NotAtt_Bsln_Low = Baseline_Low_Attended - Baseline_Low_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition high rewarded

Diff_Att_NotAtt_Acq_High = Acquisition_High_Attended - Acquisition_High_NotAttended
plotPost(Diff_Att_NotAtt_Acq_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition low rewarded

Diff_Att_NotAtt_Acq_Low = Acquisition_Low_Attended - Acquisition_Low_NotAttended
plotPost(Diff_Att_NotAtt_Acq_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction high rewarded

Diff_Att_NotAtt_Ext_High = Extinction_High_Attended - Extinction_High_NotAttended
plotPost(Diff_Att_NotAtt_Ext_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction low rewarded

Diff_Att_NotAtt_Ext_Low = Extinction_Low_Attended - Extinction_Low_NotAttended
plotPost(Diff_Att_NotAtt_Ext_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Comparison between phases

Check the difference between baseline and acquisition in high reward attended

Diff_Bsln_Acq_High_Att = Baseline_High_Attended - Acquisition_High_Attended
plotPost(Diff_Bsln_Acq_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward attended

Diff_Bsln_Acq_Low_Att = Baseline_Low_Attended - Acquisition_Low_Attended
plotPost(Diff_Bsln_Acq_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward not attended

Diff_Bsln_Acq_High_NotAtt = Baseline_High_NotAttended - Acquisition_High_NotAttended
plotPost(Diff_Bsln_Acq_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward not attended

Diff_Bsln_Acq_Low_NotAtt = Acquisition_Low_NotAttended - Baseline_Low_NotAttended  
plotPost(Diff_Bsln_Acq_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward attended

Diff_Acq_Ext_High_Att = Acquisition_High_Attended - Extinction_High_Attended
plotPost(Diff_Acq_Ext_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward attended

Diff_Acq_Ext_Low_Att = Extinction_Low_Attended - Acquisition_Low_Attended 
plotPost(Diff_Acq_Ext_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward not attended

Diff_Acq_Ext_High_NotAtt = Extinction_High_NotAttended - Acquisition_High_NotAttended 
plotPost(Diff_Acq_Ext_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward not attended

Diff_Acq_Ext_Low_NotAtt = Extinction_Low_NotAttended - Acquisition_Low_NotAttended 
plotPost(Diff_Acq_Ext_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Baseline difference

Check the difference between high and low reward in baseline attended

Diff_Bsln_High_Low_Att = Baseline_High_Attended - Baseline_Low_Attended
plotPost(Diff_Bsln_High_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between high and low reward in baseline not attended

Diff_Bsln_High_Low_NotAtt = Baseline_High_NotAttended - Baseline_Low_NotAttended
plotPost(Diff_Bsln_High_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward unattended vs. attended

Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended = Diff_Bsln_High_Low_NotAtt - Diff_Bsln_High_Low_Att 
plotPost(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[2]], digits = 2), "]")
## [1] "Mean =  0.01  [ -0.04   0.07 ]"

Movement trials

Prepare the dataset

# import data
data.raw = read.csv(file = here("data","singleTrial_amplitudes_movement_and_nomovement.csv"),header=TRUE,na.strings="NaN") 

data = data.raw

# Clean the subject name variable
data$participant = gsub('VP', '', data$participant)
data$participant = as.numeric(data$participant)

# Change the names of the variables
names(data)[names(data) == "participant"] = "Subject"
names(data)[names(data) == "amplitude"] = "Amplitude"
names(data)[names(data) == "frequency"] = "Frequency"


# Add new variables based on the condition
data$ExpPhase[data$condition == 1 | data$condition == 2 | data$condition == 11 | data$condition == 12]="Bsln"
data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

data$AttendedColor[data$condition == 1 | data$condition == 3 | data$condition == 5 | data$condition == 11 |data$condition == 13 | data$condition == 15]="Red"
data$AttendedColor[data$condition == 2 | data$condition == 4 | data$condition == 6 | data$condition == 12 |data$condition == 14 | data$condition == 16]="Blue"

data$Movement[data$condition == 1 | data$condition == 2 | data$condition == 3 | data$condition == 4 |data$condition == 5 | data$condition == 6]="NoMovement"
data$Movement[data$condition == 11 | data$condition == 12 | data$condition == 13 | data$condition == 14 |data$condition == 15 | data$condition == 16]="Movement"

data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

# Add the variable defining which color is rewarded based on the participant number
data$RewardedColor = ifelse(data$Subject%%2==0,"Blue","Red") # if participant number is even, blue was rewarded

# Switch the Frequency to the color
data$RecordedFrequency = ifelse(data$Frequency==10,"Blue","Red") # if the recorded frequency is 10Hz assign Blue (color flickering at 10Hz), otherwise assign Red (color flickering at 12Hz)

# Make a new condition based on the attended color and the rewarded color
data$Condition = ifelse(data$AttendedColor==data$RewardedColor, "High_Rew","Low_Rew")

# Make a new condition based on the attended color and the recorded frequency
data$Attention = ifelse(data$AttendedColor==data$RecordedFrequency, "Att","NotAtt")

# Make a new condition based the Condition and the Attention
data$RecordingAndCondition = with(data, paste0(Condition,"_",Attention))

# Select variables which we want to keep
data = subset(data, select=c("Subject","RewardedColor","ExpPhase","AttendedColor","Condition","RecordedFrequency","Attention","RecordingAndCondition","Amplitude","Movement"))

# Sort the data 
data = data[with(data, order(Subject)), ]

# Normalize the two frequencies
#Make a new variable with mean amplitude across all conditions for each participant and each frequency  !!! originally did not include ExpPhase below !!!
data = ddply(data,.(Subject,RecordedFrequency),transform,
                    MeanAmplitude = mean(Amplitude[ExpPhase=="Bsln"],na.rm=TRUE),
                    SDAmplitude =   sd(Amplitude,na.rm=TRUE))

# data = ddply(data,.(Subject,RecordedFrequency),transform,
#              MeanAmplitude = mean(Amplitude,na.rm=TRUE),
#              SDAmplitude =   sd(Amplitude,na.rm=TRUE))

#MeanAmplitude = mean(Amplitude[ExpPhase=="Baseline"],na.rm=TRUE),   [ExpPhase=="Bsln"]

# Divide amplitudes in each Subject, Frequency, and Condition by the Mean Amplitude
data$Amplitude = data$Amplitude/data$MeanAmplitude

# Calculate the attention indexes - Selectivity (attended-unattended) & total enhancement (attended+unattended) (Andersen & Muller, 2010, PNAS)
data.diff = ddply(data, .(Subject,ExpPhase,Condition), transform, Selectivity = Amplitude[Attention=="Att"]-Amplitude[Attention=="NotAtt"],TotalEnhancement=Amplitude[Attention=="Att"]+Amplitude[Attention=="NotAtt"])
# Delete the Attention column and rows which are not necessary (indexes repeated twice)
data.diff = subset(data.diff,Attention=="Att") #keep only Att as it is equal to NotAtt
data.diff$Attention = NULL  #drop the Attention column

# Sort the data 
data.diff$ExpPhase = factor(data.diff$ExpPhase, levels = c("Bsln","Acq","Ext"))
data.diff = data.diff[order(data.diff$Subject,data.diff$Condition,data.diff$ExpPhase),]

# Take only the no movement trials
data = subset(data,Movement=="Movement")

Means - raw data

summary = ddply(data,.(Attention,ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Amplitude, na.rm = TRUE), digits = 2), " [", round(hdi(Amplitude)[[1]], digits = 2), " ", round(hdi(Amplitude)[[2]], digits = 2), "]")))

names(summary) = c("Attention", "Reward phase", "Reward probability", "Amplitude")

summary$Attention = dplyr::recode(summary$Attention,
                           "Att" = "Attended",
                           "NotAtt" = "Unattended")

summary$`Reward phase` = dplyr::recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = dplyr::recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

summary$`Reward phase` = factor(summary$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
summary = summary[order(summary$Attention,summary$`Reward phase`,summary$`Reward probability`),]
row.names(summary) = NULL

knitr::kable(summary, caption = "Amplitudes per condition")
Amplitudes per condition
Attention Reward phase Reward probability Amplitude
Attended Baseline High 1.06 [ 0.37 1.75 ]
Attended Baseline Low 1.05 [ 0.38 1.72 ]
Attended Acquisition High 1.08 [ 0.32 1.75 ]
Attended Acquisition Low 1.05 [ 0.38 1.77 ]
Attended Extinction High 1.09 [ 0.38 1.87 ]
Attended Extinction Low 1.07 [ 0.37 1.86 ]
Unattended Baseline High 0.92 [ 0.24 1.53 ]
Unattended Baseline Low 0.92 [ 0.3 1.55 ]
Unattended Acquisition High 0.91 [ 0.28 1.56 ]
Unattended Acquisition Low 0.9 [ 0.28 1.55 ]
Unattended Extinction High 0.93 [ 0.27 1.62 ]
Unattended Extinction Low 0.95 [ 0.28 1.68 ]

Plots - raw data

# Plot amplitude across experiment phases

# prepare data for plotting
dataPlot = data

dataPlot =  ddply(dataPlot,.(Subject,ExpPhase,Condition,Attention),summarise,
                    Amplitude=mean(Amplitude,na.rm=TRUE)) # mean RTs


# rename variables
colnames(dataPlot)[colnames(dataPlot)=="ExpPhase"] <- "Reward phase"
colnames(dataPlot)[colnames(dataPlot)=="Condition"] <- "Reward probability"

# rename conditions
dataPlot$`Reward phase` = dplyr::recode(dataPlot$`Reward phase`,
                                  "Acq" = "Acquisition",
                                  "Bsln" = "Baseline",
                                  "Ext" = "Extinction")

dataPlot$`Reward probability` = dplyr::recode(dataPlot$`Reward probability`,
                                        "High_Rew" = "High",
                                        "Low_Rew" = "Low")

#order
dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(dataPlot,Attention=="Att")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(dataPlot,Attention=="NotAtt")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.5,1.7), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color
}

Statistics - movement

# Set the working directory in order to load the models
setwd(here("brms_models"))
model.full.threefactors = readRDS("full.EEG.allsubs.movement.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.threefactors, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.threefactors)

Plotting the best model

post = posterior_samples(model.full.threefactors, "^b")

# Calculate posteriors for each condition

################################################ Baseline ####

##################### Attended

######### High reward
Baseline_High_Attended = post[["b_Intercept"]]
######### Low reward
Baseline_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

##################### Not Attended

######### High reward
Baseline_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]]
######### Low reward
Baseline_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:AttentionNotAtt"]]

################################################ Acquistion

##################### Attended

######### High reward
Acquisition_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]]

##################### Not Attended

######### High reward
Acquisition_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseAcq:AttentionNotAtt"]]
  
######### Low reward
Acquisition_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq:AttentionNotAtt"]]

################################################ Extinction

##################### Attended

######### High reward
Extinction_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt"]]

##################### Not Attended

######### High reward
Extinction_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseExt:AttentionNotAtt"]]
######### Low reward
Extinction_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt:AttentionNotAtt"]]
# make a data frame

posterior_conditions = melt(data.frame(Baseline_High_Attended, Baseline_High_NotAttended, Baseline_Low_Attended, Baseline_Low_NotAttended, Acquisition_High_Attended, Acquisition_High_NotAttended, Acquisition_Low_Attended, Acquisition_Low_NotAttended, Extinction_High_Attended, Extinction_High_NotAttended, Extinction_Low_Attended, Extinction_Low_NotAttended))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability", "Attention"), "_", extra = "merge")

posterior_conditions$Attention = recode(posterior_conditions$Attention,
                           "Attended" = "Attended",
                           "NotAttended" = "Unattended")

names(posterior_conditions)[4] = "Amplitude"


#order
#dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
#dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Attended")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Unattended")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.7,1.3), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color
}

Table of means across conditions

# Make a table with conditions
posterior_means = as.data.frame(c("Attended Baseline High Reward", 
                                  "Attended Baseline Low Reward", 
                                  "Attended Acquisition High Reward", 
                                  "Attended Acquisition Low Reward", 
                                  "Attended Extinction High Reward", 
                                  "Attended Extinction Low Reward",
                                  "Unattended Baseline High Reward", 
                                  "Unattended Baseline Low Reward", 
                                  "Unattended Acquisition High Reward", 
                                  "Unattended Acquisition Low Reward", 
                                  "Unattended Extinction High Reward", 
                                  "Unattended Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High_Attended), digits = 2), " [", round(hdi(Baseline_High_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_Attended), digits = 2), " [", round(hdi(Baseline_Low_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_Attended)[[2]], digits = 2), "]"),
                        
                        paste(round(mean(Acquisition_High_Attended), digits = 2), " [", round(hdi(Acquisition_High_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_Attended), digits = 2), " [", round(hdi(Acquisition_Low_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_Attended), digits = 2), " [", round(hdi(Extinction_High_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_Attended), digits = 2), " [", round(hdi(Extinction_Low_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_High_NotAttended), digits = 2), " [", round(hdi(Baseline_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_NotAttended), digits = 2), " [", round(hdi(Baseline_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High_NotAttended), digits = 2), " [", round(hdi(Acquisition_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_NotAttended), digits = 2), " [", round(hdi(Acquisition_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_NotAttended), digits = 2), " [", round(hdi(Extinction_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_NotAttended), digits = 2), " [", round(hdi(Extinction_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_NotAttended)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Attention", "Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Attention Reward Phase Reward Probability Mean [HDI]
Attended Baseline High Reward 1.06 [ 1.04 1.09 ]
Attended Baseline Low Reward 1.05 [ 1.02 1.08 ]
Attended Acquisition High Reward 1.08 [ 1.05 1.12 ]
Attended Acquisition Low Reward 1.05 [ 1.02 1.09 ]
Attended Extinction High Reward 1.09 [ 1.06 1.12 ]
Attended Extinction Low Reward 1.07 [ 1.04 1.1 ]
Unattended Baseline High Reward 0.92 [ 0.89 0.95 ]
Unattended Baseline Low Reward 0.92 [ 0.89 0.94 ]
Unattended Acquisition High Reward 0.91 [ 0.88 0.94 ]
Unattended Acquisition Low Reward 0.89 [ 0.85 0.93 ]
Unattended Extinction High Reward 0.93 [ 0.9 0.96 ]
Unattended Extinction Low Reward 0.94 [ 0.89 0.99 ]

Inference about the best model

Attended vs. unattended

Check the difference between attended and not attended in baseline high rewarded

Diff_Att_NotAtt_Bsln_High = Baseline_High_Attended - Baseline_High_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in baseline low rewarded

Diff_Att_NotAtt_Bsln_Low = Baseline_Low_Attended - Baseline_Low_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition high rewarded

Diff_Att_NotAtt_Acq_High = Acquisition_High_Attended - Acquisition_High_NotAttended
plotPost(Diff_Att_NotAtt_Acq_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition low rewarded

Diff_Att_NotAtt_Acq_Low = Acquisition_Low_Attended - Acquisition_Low_NotAttended
plotPost(Diff_Att_NotAtt_Acq_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction high rewarded

Diff_Att_NotAtt_Ext_High = Extinction_High_Attended - Extinction_High_NotAttended
plotPost(Diff_Att_NotAtt_Ext_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction low rewarded

Diff_Att_NotAtt_Ext_Low = Extinction_Low_Attended - Extinction_Low_NotAttended
plotPost(Diff_Att_NotAtt_Ext_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Comparison between phases

Check the difference between baseline and acquisition in high reward attended

Diff_Bsln_Acq_High_Att = Baseline_High_Attended - Acquisition_High_Attended
plotPost(Diff_Bsln_Acq_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward attended

Diff_Bsln_Acq_Low_Att = Baseline_Low_Attended - Acquisition_Low_Attended
plotPost(Diff_Bsln_Acq_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward not attended

Diff_Bsln_Acq_High_NotAtt = Baseline_High_NotAttended - Acquisition_High_NotAttended
plotPost(Diff_Bsln_Acq_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward not attended

Diff_Bsln_Acq_Low_NotAtt = Acquisition_Low_NotAttended - Baseline_Low_NotAttended  
plotPost(Diff_Bsln_Acq_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward attended

Diff_Acq_Ext_High_Att = Acquisition_High_Attended - Extinction_High_Attended
plotPost(Diff_Acq_Ext_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward attended

Diff_Acq_Ext_Low_Att = Extinction_Low_Attended - Acquisition_Low_Attended 
plotPost(Diff_Acq_Ext_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward not attended

Diff_Acq_Ext_High_NotAtt = Extinction_High_NotAttended - Acquisition_High_NotAttended 
plotPost(Diff_Acq_Ext_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward not attended

Diff_Acq_Ext_Low_NotAtt = Extinction_Low_NotAttended - Acquisition_Low_NotAttended 
plotPost(Diff_Acq_Ext_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Baseline difference

Check the difference between high and low reward in baseline attended

Diff_Bsln_High_Low_Att = Baseline_High_Attended - Baseline_Low_Attended
plotPost(Diff_Bsln_High_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between high and low reward in baseline not attended

Diff_Bsln_High_Low_NotAtt = Baseline_High_NotAttended - Baseline_Low_NotAttended
plotPost(Diff_Bsln_High_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward unattended vs. attended

Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended = Diff_Bsln_High_Low_NotAtt - Diff_Bsln_High_Low_Att 
plotPost(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[2]], digits = 2), "]")
## [1] "Mean =  -0.01  [ -0.04   0.02 ]"

EEG - average normalization

Topography & spectra

Topography and spectra

Topography and spectra

All trials

Prepare the dataset

# import data
data.raw = read.csv(file = here("data","singleTrial_amplitudes_movement_and_nomovement.csv"),header=TRUE,na.strings="NaN") 

data = data.raw

# Clean the subject name variable
data$participant = gsub('VP', '', data$participant)
data$participant = as.numeric(data$participant)

# Change the names of the variables
names(data)[names(data) == "participant"] = "Subject"
names(data)[names(data) == "amplitude"] = "Amplitude"
names(data)[names(data) == "frequency"] = "Frequency"


# Add new variables based on the condition
data$ExpPhase[data$condition == 1 | data$condition == 2 | data$condition == 11 | data$condition == 12]="Bsln"
data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

data$AttendedColor[data$condition == 1 | data$condition == 3 | data$condition == 5 | data$condition == 11 |data$condition == 13 | data$condition == 15]="Red"
data$AttendedColor[data$condition == 2 | data$condition == 4 | data$condition == 6 | data$condition == 12 |data$condition == 14 | data$condition == 16]="Blue"

data$Movement[data$condition == 1 | data$condition == 2 | data$condition == 3 | data$condition == 4 |data$condition == 5 | data$condition == 6]="NoMovement"
data$Movement[data$condition == 11 | data$condition == 12 | data$condition == 13 | data$condition == 14 |data$condition == 15 | data$condition == 16]="Movement"

data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

# Add the variable defining which color is rewarded based on the participant number
data$RewardedColor = ifelse(data$Subject%%2==0,"Blue","Red") # if participant number is even, blue was rewarded

# Switch the Frequency to the color
data$RecordedFrequency = ifelse(data$Frequency==10,"Blue","Red") # if the recorded frequency is 10Hz assign Blue (color flickering at 10Hz), otherwise assign Red (color flickering at 12Hz)

# Make a new condition based on the attended color and the rewarded color
data$Condition = ifelse(data$AttendedColor==data$RewardedColor, "High_Rew","Low_Rew")

# Make a new condition based on the attended color and the recorded frequency
data$Attention = ifelse(data$AttendedColor==data$RecordedFrequency, "Att","NotAtt")

# Make a new condition based the Condition and the Attention
data$RecordingAndCondition = with(data, paste0(Condition,"_",Attention))

# Select variables which we want to keep
data = subset(data, select=c("Subject","RewardedColor","ExpPhase","AttendedColor","Condition","RecordedFrequency","Attention","RecordingAndCondition","Amplitude","Movement"))

# Sort the data 
data = data[with(data, order(Subject)), ]

# Normalize the two frequencies
#Make a new variable with mean amplitude across all conditions for each participant and each frequency  !!! originally did not include ExpPhase below !!!
# data = ddply(data,.(Subject,RecordedFrequency),transform,
#                     MeanAmplitude = mean(Amplitude[ExpPhase=="Bsln"],na.rm=TRUE),
#                     SDAmplitude =   sd(Amplitude,na.rm=TRUE))

data = ddply(data,.(Subject,RecordedFrequency),transform,
             MeanAmplitude = mean(Amplitude,na.rm=TRUE),
             SDAmplitude =   sd(Amplitude,na.rm=TRUE))

#MeanAmplitude = mean(Amplitude[ExpPhase=="Baseline"],na.rm=TRUE),   [ExpPhase=="Bsln"]

# Divide amplitudes in each Subject, Frequency, and Condition by the Mean Amplitude
data$Amplitude = data$Amplitude/data$MeanAmplitude

# Calculate the attention indexes - Selectivity (attended-unattended) & total enhancement (attended+unattended) (Andersen & Muller, 2010, PNAS)
data.diff = ddply(data, .(Subject,ExpPhase,Condition), transform, Selectivity = Amplitude[Attention=="Att"]-Amplitude[Attention=="NotAtt"],TotalEnhancement=Amplitude[Attention=="Att"]+Amplitude[Attention=="NotAtt"])
# Delete the Attention column and rows which are not necessary (indexes repeated twice)
data.diff = subset(data.diff,Attention=="Att") #keep only Att as it is equal to NotAtt
data.diff$Attention = NULL  #drop the Attention column

# Sort the data 
data.diff$ExpPhase = factor(data.diff$ExpPhase, levels = c("Bsln","Acq","Ext"))
data.diff = data.diff[order(data.diff$Subject,data.diff$Condition,data.diff$ExpPhase),]

# # Calculate the reward index - High reward minus Low reward
# data.reward = ddply(data, .(Subject,ExpPhase,Attention), transform, Reward = Amplitude[Condition=="High_Rew"]-Amplitude[Condition=="Low_Rew"])
# # Delete the Attention column and rows which are not necessary (indexes repeated twice)
# data.reward = subset(data.reward,Condition=="High_Rew") #keep only Att as it is equal to NotAtt
# data.reward$Condition = NULL  #drop the Condition column
# 
# # Sort the data 
# data.reward$ExpPhase = factor(data.reward$ExpPhase, levels = c("Bsln","Acq","Ext"))
# data.reward = data.reward[order(data.reward$Subject,data.reward$Attention,data.reward$ExpPhase),]

Means - raw data

summary = ddply(data,.(Attention,ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Amplitude, na.rm = TRUE), digits = 2), " [", round(hdi(Amplitude)[[1]], digits = 2), " ", round(hdi(Amplitude)[[2]], digits = 2), "]")))

names(summary) = c("Attention", "Reward phase", "Reward probability", "Amplitude")

summary$Attention = dplyr::recode(summary$Attention,
                           "Att" = "Attended",
                           "NotAtt" = "Unattended")

summary$`Reward phase` = dplyr::recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = dplyr::recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

summary$`Reward phase` = factor(summary$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
summary = summary[order(summary$Attention,summary$`Reward phase`,summary$`Reward probability`),]
row.names(summary) = NULL

knitr::kable(summary, caption = "Amplitudes per condition")
Amplitudes per condition
Attention Reward phase Reward probability Amplitude
Attended Baseline High 1.07 [ 0.35 1.76 ]
Attended Baseline Low 1.06 [ 0.37 1.79 ]
Attended Acquisition High 1.07 [ 0.39 1.77 ]
Attended Acquisition Low 1.06 [ 0.37 1.77 ]
Attended Extinction High 1.09 [ 0.35 1.85 ]
Attended Extinction Low 1.08 [ 0.33 1.81 ]
Unattended Baseline High 0.93 [ 0.29 1.61 ]
Unattended Baseline Low 0.92 [ 0.29 1.6 ]
Unattended Acquisition High 0.92 [ 0.31 1.63 ]
Unattended Acquisition Low 0.9 [ 0.27 1.54 ]
Unattended Extinction High 0.95 [ 0.3 1.68 ]
Unattended Extinction Low 0.96 [ 0.27 1.7 ]

Plots - raw data

# Plot amplitude across experiment phases

# prepare data for plotting
dataPlot = data

dataPlot =  ddply(dataPlot,.(Subject,ExpPhase,Condition,Attention),summarise,
                    Amplitude=mean(Amplitude,na.rm=TRUE)) # mean RTs


# rename variables
colnames(dataPlot)[colnames(dataPlot)=="ExpPhase"] <- "Reward phase"
colnames(dataPlot)[colnames(dataPlot)=="Condition"] <- "Reward probability"

# rename conditions
dataPlot$`Reward phase` = dplyr::recode(dataPlot$`Reward phase`,
                                  "Acq" = "Acquisition",
                                  "Bsln" = "Baseline",
                                  "Ext" = "Extinction")

dataPlot$`Reward probability` = dplyr::recode(dataPlot$`Reward probability`,
                                        "High_Rew" = "High",
                                        "Low_Rew" = "Low")

#order
dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(dataPlot,Attention=="Att")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(dataPlot,Attention=="NotAtt")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.5,1.7), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color
}

Statistics

# Set the working directory in order to load the models
setwd(here("brms_models"))
model.full.threefactors = readRDS("full.EEG.allsubs_old_normalization.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.threefactors, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.threefactors)

Plotting the best model

post = posterior_samples(model.full.threefactors, "^b")

# Calculate posteriors for each condition

################################################ Baseline ####

##################### Attended

######### High reward
Baseline_High_Attended = post[["b_Intercept"]]
######### Low reward
Baseline_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

##################### Not Attended

######### High reward
Baseline_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]]
######### Low reward
Baseline_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:AttentionNotAtt"]]

################################################ Acquistion

##################### Attended

######### High reward
Acquisition_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]]

##################### Not Attended

######### High reward
Acquisition_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseAcq:AttentionNotAtt"]]
  
######### Low reward
Acquisition_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq:AttentionNotAtt"]]

################################################ Extinction

##################### Attended

######### High reward
Extinction_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt"]]

##################### Not Attended

######### High reward
Extinction_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseExt:AttentionNotAtt"]]
######### Low reward
Extinction_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt:AttentionNotAtt"]]
# make a data frame

posterior_conditions = melt(data.frame(Baseline_High_Attended, Baseline_High_NotAttended, Baseline_Low_Attended, Baseline_Low_NotAttended, Acquisition_High_Attended, Acquisition_High_NotAttended, Acquisition_Low_Attended, Acquisition_Low_NotAttended, Extinction_High_Attended, Extinction_High_NotAttended, Extinction_Low_Attended, Extinction_Low_NotAttended))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability", "Attention"), "_", extra = "merge")

posterior_conditions$Attention = recode(posterior_conditions$Attention,
                           "Attended" = "Attended",
                           "NotAttended" = "Unattended")

names(posterior_conditions)[4] = "Amplitude"


#order
#dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
#dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Attended")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Unattended")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.7,1.3), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color
}

Table of means across conditions

# Make a table with conditions
posterior_means = as.data.frame(c("Attended Baseline High Reward", 
                                  "Attended Baseline Low Reward", 
                                  "Attended Acquisition High Reward", 
                                  "Attended Acquisition Low Reward", 
                                  "Attended Extinction High Reward", 
                                  "Attended Extinction Low Reward",
                                  "Unattended Baseline High Reward", 
                                  "Unattended Baseline Low Reward", 
                                  "Unattended Acquisition High Reward", 
                                  "Unattended Acquisition Low Reward", 
                                  "Unattended Extinction High Reward", 
                                  "Unattended Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High_Attended), digits = 2), " [", round(hdi(Baseline_High_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_Attended), digits = 2), " [", round(hdi(Baseline_Low_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_Attended)[[2]], digits = 2), "]"),
                        
                        paste(round(mean(Acquisition_High_Attended), digits = 2), " [", round(hdi(Acquisition_High_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_Attended), digits = 2), " [", round(hdi(Acquisition_Low_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_Attended), digits = 2), " [", round(hdi(Extinction_High_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_Attended), digits = 2), " [", round(hdi(Extinction_Low_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_High_NotAttended), digits = 2), " [", round(hdi(Baseline_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_NotAttended), digits = 2), " [", round(hdi(Baseline_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High_NotAttended), digits = 2), " [", round(hdi(Acquisition_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_NotAttended), digits = 2), " [", round(hdi(Acquisition_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_NotAttended), digits = 2), " [", round(hdi(Extinction_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_NotAttended), digits = 2), " [", round(hdi(Extinction_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_NotAttended)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Attention", "Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Attention Reward Phase Reward Probability Mean [HDI]
Attended Baseline High Reward 1.07 [ 1.04 1.09 ]
Attended Baseline Low Reward 1.06 [ 1.04 1.09 ]
Attended Acquisition High Reward 1.07 [ 1.04 1.1 ]
Attended Acquisition Low Reward 1.06 [ 1.03 1.09 ]
Attended Extinction High Reward 1.09 [ 1.06 1.11 ]
Attended Extinction Low Reward 1.08 [ 1.06 1.11 ]
Unattended Baseline High Reward 0.93 [ 0.9 0.95 ]
Unattended Baseline Low Reward 0.92 [ 0.89 0.94 ]
Unattended Acquisition High Reward 0.92 [ 0.9 0.95 ]
Unattended Acquisition Low Reward 0.91 [ 0.87 0.94 ]
Unattended Extinction High Reward 0.95 [ 0.92 0.98 ]
Unattended Extinction Low Reward 0.96 [ 0.92 1 ]

Inference about the best model

Attended vs. unattended

Check the difference between attended and not attended in baseline high rewarded

Diff_Att_NotAtt_Bsln_High = Baseline_High_Attended - Baseline_High_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in baseline low rewarded

Diff_Att_NotAtt_Bsln_Low = Baseline_Low_Attended - Baseline_Low_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition high rewarded

Diff_Att_NotAtt_Acq_High = Acquisition_High_Attended - Acquisition_High_NotAttended
plotPost(Diff_Att_NotAtt_Acq_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition low rewarded

Diff_Att_NotAtt_Acq_Low = Acquisition_Low_Attended - Acquisition_Low_NotAttended
plotPost(Diff_Att_NotAtt_Acq_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction high rewarded

Diff_Att_NotAtt_Ext_High = Extinction_High_Attended - Extinction_High_NotAttended
plotPost(Diff_Att_NotAtt_Ext_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction low rewarded

Diff_Att_NotAtt_Ext_Low = Extinction_Low_Attended - Extinction_Low_NotAttended
plotPost(Diff_Att_NotAtt_Ext_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Comparison between phases

Check the difference between baseline and acquisition in high reward attended

Diff_Bsln_Acq_High_Att = Baseline_High_Attended - Acquisition_High_Attended
plotPost(Diff_Bsln_Acq_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward attended

Diff_Bsln_Acq_Low_Att = Baseline_Low_Attended - Acquisition_Low_Attended
plotPost(Diff_Bsln_Acq_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward not attended

Diff_Bsln_Acq_High_NotAtt = Baseline_High_NotAttended - Acquisition_High_NotAttended
plotPost(Diff_Bsln_Acq_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward not attended

Diff_Bsln_Acq_Low_NotAtt = Acquisition_Low_NotAttended - Baseline_Low_NotAttended  
plotPost(Diff_Bsln_Acq_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward attended

Diff_Acq_Ext_High_Att = Acquisition_High_Attended - Extinction_High_Attended
plotPost(Diff_Acq_Ext_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward attended

Diff_Acq_Ext_Low_Att = Extinction_Low_Attended - Acquisition_Low_Attended 
plotPost(Diff_Acq_Ext_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward not attended

Diff_Acq_Ext_High_NotAtt = Extinction_High_NotAttended - Acquisition_High_NotAttended 
plotPost(Diff_Acq_Ext_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward not attended

Diff_Acq_Ext_Low_NotAtt = Extinction_Low_NotAttended - Acquisition_Low_NotAttended 
plotPost(Diff_Acq_Ext_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Baseline difference

Check the difference between high and low reward in baseline attended

Diff_Bsln_High_Low_Att = Baseline_High_Attended - Baseline_Low_Attended
plotPost(Diff_Bsln_High_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between high and low reward in baseline not attended

Diff_Bsln_High_Low_NotAtt = Baseline_High_NotAttended - Baseline_Low_NotAttended
plotPost(Diff_Bsln_High_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward unattended vs. attended

Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended = Diff_Bsln_High_Low_NotAtt - Diff_Bsln_High_Low_Att 
plotPost(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[2]], digits = 2), "]")
## [1] "Mean =  0.01  [ -0.02   0.03 ]"

No movement trials

Prepare the dataset

# import data
data.raw = read.csv(file = here("data","singleTrial_amplitudes_movement_and_nomovement.csv"),header=TRUE,na.strings="NaN") 

data = data.raw

# Clean the subject name variable
data$participant = gsub('VP', '', data$participant)
data$participant = as.numeric(data$participant)

# Change the names of the variables
names(data)[names(data) == "participant"] = "Subject"
names(data)[names(data) == "amplitude"] = "Amplitude"
names(data)[names(data) == "frequency"] = "Frequency"


# Add new variables based on the condition
data$ExpPhase[data$condition == 1 | data$condition == 2 | data$condition == 11 | data$condition == 12]="Bsln"
data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

data$AttendedColor[data$condition == 1 | data$condition == 3 | data$condition == 5 | data$condition == 11 |data$condition == 13 | data$condition == 15]="Red"
data$AttendedColor[data$condition == 2 | data$condition == 4 | data$condition == 6 | data$condition == 12 |data$condition == 14 | data$condition == 16]="Blue"

data$Movement[data$condition == 1 | data$condition == 2 | data$condition == 3 | data$condition == 4 |data$condition == 5 | data$condition == 6]="NoMovement"
data$Movement[data$condition == 11 | data$condition == 12 | data$condition == 13 | data$condition == 14 |data$condition == 15 | data$condition == 16]="Movement"

data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

# Add the variable defining which color is rewarded based on the participant number
data$RewardedColor = ifelse(data$Subject%%2==0,"Blue","Red") # if participant number is even, blue was rewarded

# Switch the Frequency to the color
data$RecordedFrequency = ifelse(data$Frequency==10,"Blue","Red") # if the recorded frequency is 10Hz assign Blue (color flickering at 10Hz), otherwise assign Red (color flickering at 12Hz)

# Make a new condition based on the attended color and the rewarded color
data$Condition = ifelse(data$AttendedColor==data$RewardedColor, "High_Rew","Low_Rew")

# Make a new condition based on the attended color and the recorded frequency
data$Attention = ifelse(data$AttendedColor==data$RecordedFrequency, "Att","NotAtt")

# Make a new condition based the Condition and the Attention
data$RecordingAndCondition = with(data, paste0(Condition,"_",Attention))

# Select variables which we want to keep
data = subset(data, select=c("Subject","RewardedColor","ExpPhase","AttendedColor","Condition","RecordedFrequency","Attention","RecordingAndCondition","Amplitude","Movement"))

# Sort the data 
data = data[with(data, order(Subject)), ]

# Normalize the two frequencies
#Make a new variable with mean amplitude across all conditions for each participant and each frequency  !!! originally did not include ExpPhase below !!!
# data = ddply(data,.(Subject,RecordedFrequency),transform,
#                     MeanAmplitude = mean(Amplitude[ExpPhase=="Bsln"],na.rm=TRUE),
#                     SDAmplitude =   sd(Amplitude,na.rm=TRUE))

data = ddply(data,.(Subject,RecordedFrequency),transform,
             MeanAmplitude = mean(Amplitude,na.rm=TRUE),
             SDAmplitude =   sd(Amplitude,na.rm=TRUE))

#MeanAmplitude = mean(Amplitude[ExpPhase=="Baseline"],na.rm=TRUE),   [ExpPhase=="Bsln"]

# Divide amplitudes in each Subject, Frequency, and Condition by the Mean Amplitude
data$Amplitude = data$Amplitude/data$MeanAmplitude

# Calculate the attention indexes - Selectivity (attended-unattended) & total enhancement (attended+unattended) (Andersen & Muller, 2010, PNAS)
data.diff = ddply(data, .(Subject,ExpPhase,Condition), transform, Selectivity = Amplitude[Attention=="Att"]-Amplitude[Attention=="NotAtt"],TotalEnhancement=Amplitude[Attention=="Att"]+Amplitude[Attention=="NotAtt"])
# Delete the Attention column and rows which are not necessary (indexes repeated twice)
data.diff = subset(data.diff,Attention=="Att") #keep only Att as it is equal to NotAtt
data.diff$Attention = NULL  #drop the Attention column

# Sort the data 
data.diff$ExpPhase = factor(data.diff$ExpPhase, levels = c("Bsln","Acq","Ext"))
data.diff = data.diff[order(data.diff$Subject,data.diff$Condition,data.diff$ExpPhase),]

# Take only the no movement trials
data = subset(data,Movement=="NoMovement")

Means - raw data

summary = ddply(data,.(Attention,ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Amplitude, na.rm = TRUE), digits = 2), " [", round(hdi(Amplitude)[[1]], digits = 2), " ", round(hdi(Amplitude)[[2]], digits = 2), "]")))

names(summary) = c("Attention", "Reward phase", "Reward probability", "Amplitude")

summary$Attention = dplyr::recode(summary$Attention,
                           "Att" = "Attended",
                           "NotAtt" = "Unattended")

summary$`Reward phase` = dplyr::recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = dplyr::recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

summary$`Reward phase` = factor(summary$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
summary = summary[order(summary$Attention,summary$`Reward phase`,summary$`Reward probability`),]
row.names(summary) = NULL

knitr::kable(summary, caption = "Amplitudes per condition")
Amplitudes per condition
Attention Reward phase Reward probability Amplitude
Attended Baseline High 1.09 [ 0.36 1.83 ]
Attended Baseline Low 1.09 [ 0.37 1.84 ]
Attended Acquisition High 1.07 [ 0.4 1.72 ]
Attended Acquisition Low 1.08 [ 0.37 1.77 ]
Attended Extinction High 1.1 [ 0.39 1.9 ]
Attended Extinction Low 1.11 [ 0.34 1.86 ]
Unattended Baseline High 0.95 [ 0.29 1.65 ]
Unattended Baseline Low 0.93 [ 0.29 1.64 ]
Unattended Acquisition High 0.95 [ 0.32 1.7 ]
Unattended Acquisition Low 0.92 [ 0.33 1.6 ]
Unattended Extinction High 0.98 [ 0.32 1.78 ]
Unattended Extinction Low 0.98 [ 0.28 1.76 ]

Plots - raw data

# Plot amplitude across experiment phases

# prepare data for plotting
dataPlot = data

dataPlot =  ddply(dataPlot,.(Subject,ExpPhase,Condition,Attention),summarise,
                    Amplitude=mean(Amplitude,na.rm=TRUE)) # mean RTs


# rename variables
colnames(dataPlot)[colnames(dataPlot)=="ExpPhase"] <- "Reward phase"
colnames(dataPlot)[colnames(dataPlot)=="Condition"] <- "Reward probability"

# rename conditions
dataPlot$`Reward phase` = dplyr::recode(dataPlot$`Reward phase`,
                                  "Acq" = "Acquisition",
                                  "Bsln" = "Baseline",
                                  "Ext" = "Extinction")

dataPlot$`Reward probability` = dplyr::recode(dataPlot$`Reward probability`,
                                        "High_Rew" = "High",
                                        "Low_Rew" = "Low")

#order
dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(dataPlot,Attention=="Att")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(dataPlot,Attention=="NotAtt")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.5,1.7), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color
}

Statistics - no movement trials

# Set the working directory in order to load the models
setwd(here("brms_models"))
model.full.threefactors = readRDS("full.EEG.allsubs.nomovement_old_normalization.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.threefactors, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.threefactors)

Plotting the best model

post = posterior_samples(model.full.threefactors, "^b")

# Calculate posteriors for each condition

################################################ Baseline ####

##################### Attended

######### High reward
Baseline_High_Attended = post[["b_Intercept"]]
######### Low reward
Baseline_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

##################### Not Attended

######### High reward
Baseline_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]]
######### Low reward
Baseline_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:AttentionNotAtt"]]

################################################ Acquistion

##################### Attended

######### High reward
Acquisition_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]]

##################### Not Attended

######### High reward
Acquisition_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseAcq:AttentionNotAtt"]]
  
######### Low reward
Acquisition_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq:AttentionNotAtt"]]

################################################ Extinction

##################### Attended

######### High reward
Extinction_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt"]]

##################### Not Attended

######### High reward
Extinction_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseExt:AttentionNotAtt"]]
######### Low reward
Extinction_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt:AttentionNotAtt"]]
# make a data frame

posterior_conditions = melt(data.frame(Baseline_High_Attended, Baseline_High_NotAttended, Baseline_Low_Attended, Baseline_Low_NotAttended, Acquisition_High_Attended, Acquisition_High_NotAttended, Acquisition_Low_Attended, Acquisition_Low_NotAttended, Extinction_High_Attended, Extinction_High_NotAttended, Extinction_Low_Attended, Extinction_Low_NotAttended))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability", "Attention"), "_", extra = "merge")

posterior_conditions$Attention = recode(posterior_conditions$Attention,
                           "Attended" = "Attended",
                           "NotAttended" = "Unattended")

names(posterior_conditions)[4] = "Amplitude"


#order
#dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
#dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Attended")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Unattended")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.7,1.3), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color
}

Table of means across conditions

# Make a table with conditions
posterior_means = as.data.frame(c("Attended Baseline High Reward", 
                                  "Attended Baseline Low Reward", 
                                  "Attended Acquisition High Reward", 
                                  "Attended Acquisition Low Reward", 
                                  "Attended Extinction High Reward", 
                                  "Attended Extinction Low Reward",
                                  "Unattended Baseline High Reward", 
                                  "Unattended Baseline Low Reward", 
                                  "Unattended Acquisition High Reward", 
                                  "Unattended Acquisition Low Reward", 
                                  "Unattended Extinction High Reward", 
                                  "Unattended Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High_Attended), digits = 2), " [", round(hdi(Baseline_High_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_Attended), digits = 2), " [", round(hdi(Baseline_Low_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_Attended)[[2]], digits = 2), "]"),
                        
                        paste(round(mean(Acquisition_High_Attended), digits = 2), " [", round(hdi(Acquisition_High_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_Attended), digits = 2), " [", round(hdi(Acquisition_Low_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_Attended), digits = 2), " [", round(hdi(Extinction_High_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_Attended), digits = 2), " [", round(hdi(Extinction_Low_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_High_NotAttended), digits = 2), " [", round(hdi(Baseline_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_NotAttended), digits = 2), " [", round(hdi(Baseline_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High_NotAttended), digits = 2), " [", round(hdi(Acquisition_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_NotAttended), digits = 2), " [", round(hdi(Acquisition_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_NotAttended), digits = 2), " [", round(hdi(Extinction_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_NotAttended), digits = 2), " [", round(hdi(Extinction_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_NotAttended)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Attention", "Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Attention Reward Phase Reward Probability Mean [HDI]
Attended Baseline High Reward 1.09 [ 1.06 1.11 ]
Attended Baseline Low Reward 1.09 [ 1.06 1.12 ]
Attended Acquisition High Reward 1.07 [ 1.04 1.1 ]
Attended Acquisition Low Reward 1.08 [ 1.05 1.1 ]
Attended Extinction High Reward 1.1 [ 1.07 1.14 ]
Attended Extinction Low Reward 1.12 [ 1.08 1.15 ]
Unattended Baseline High Reward 0.95 [ 0.91 0.98 ]
Unattended Baseline Low Reward 0.93 [ 0.89 0.97 ]
Unattended Acquisition High Reward 0.95 [ 0.92 0.98 ]
Unattended Acquisition Low Reward 0.94 [ 0.89 0.98 ]
Unattended Extinction High Reward 0.98 [ 0.94 1.02 ]
Unattended Extinction Low Reward 1 [ 0.94 1.05 ]

Inference about the best model

Attended vs. unattended

Check the difference between attended and not attended in baseline high rewarded

Diff_Att_NotAtt_Bsln_High = Baseline_High_Attended - Baseline_High_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in baseline low rewarded

Diff_Att_NotAtt_Bsln_Low = Baseline_Low_Attended - Baseline_Low_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition high rewarded

Diff_Att_NotAtt_Acq_High = Acquisition_High_Attended - Acquisition_High_NotAttended
plotPost(Diff_Att_NotAtt_Acq_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition low rewarded

Diff_Att_NotAtt_Acq_Low = Acquisition_Low_Attended - Acquisition_Low_NotAttended
plotPost(Diff_Att_NotAtt_Acq_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction high rewarded

Diff_Att_NotAtt_Ext_High = Extinction_High_Attended - Extinction_High_NotAttended
plotPost(Diff_Att_NotAtt_Ext_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction low rewarded

Diff_Att_NotAtt_Ext_Low = Extinction_Low_Attended - Extinction_Low_NotAttended
plotPost(Diff_Att_NotAtt_Ext_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Comparison between phases

Check the difference between baseline and acquisition in high reward attended

Diff_Bsln_Acq_High_Att = Baseline_High_Attended - Acquisition_High_Attended
plotPost(Diff_Bsln_Acq_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward attended

Diff_Bsln_Acq_Low_Att = Baseline_Low_Attended - Acquisition_Low_Attended
plotPost(Diff_Bsln_Acq_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward not attended

Diff_Bsln_Acq_High_NotAtt = Baseline_High_NotAttended - Acquisition_High_NotAttended
plotPost(Diff_Bsln_Acq_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward not attended

Diff_Bsln_Acq_Low_NotAtt = Acquisition_Low_NotAttended - Baseline_Low_NotAttended  
plotPost(Diff_Bsln_Acq_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward attended

Diff_Acq_Ext_High_Att = Acquisition_High_Attended - Extinction_High_Attended
plotPost(Diff_Acq_Ext_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward attended

Diff_Acq_Ext_Low_Att = Extinction_Low_Attended - Acquisition_Low_Attended 
plotPost(Diff_Acq_Ext_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward not attended

Diff_Acq_Ext_High_NotAtt = Extinction_High_NotAttended - Acquisition_High_NotAttended 
plotPost(Diff_Acq_Ext_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward not attended

Diff_Acq_Ext_Low_NotAtt = Extinction_Low_NotAttended - Acquisition_Low_NotAttended 
plotPost(Diff_Acq_Ext_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Baseline difference

Check the difference between high and low reward in baseline attended

Diff_Bsln_High_Low_Att = Baseline_High_Attended - Baseline_Low_Attended
plotPost(Diff_Bsln_High_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between high and low reward in baseline not attended

Diff_Bsln_High_Low_NotAtt = Baseline_High_NotAttended - Baseline_Low_NotAttended
plotPost(Diff_Bsln_High_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward unattended vs. attended

Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended = Diff_Bsln_High_Low_NotAtt - Diff_Bsln_High_Low_Att 
plotPost(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[2]], digits = 2), "]")
## [1] "Mean =  0.02  [ -0.02   0.06 ]"

Movement trials

Prepare the dataset

# import data
data.raw = read.csv(file = here("data","singleTrial_amplitudes_movement_and_nomovement.csv"),header=TRUE,na.strings="NaN") 

data = data.raw

# Clean the subject name variable
data$participant = gsub('VP', '', data$participant)
data$participant = as.numeric(data$participant)

# Change the names of the variables
names(data)[names(data) == "participant"] = "Subject"
names(data)[names(data) == "amplitude"] = "Amplitude"
names(data)[names(data) == "frequency"] = "Frequency"


# Add new variables based on the condition
data$ExpPhase[data$condition == 1 | data$condition == 2 | data$condition == 11 | data$condition == 12]="Bsln"
data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

data$AttendedColor[data$condition == 1 | data$condition == 3 | data$condition == 5 | data$condition == 11 |data$condition == 13 | data$condition == 15]="Red"
data$AttendedColor[data$condition == 2 | data$condition == 4 | data$condition == 6 | data$condition == 12 |data$condition == 14 | data$condition == 16]="Blue"

data$Movement[data$condition == 1 | data$condition == 2 | data$condition == 3 | data$condition == 4 |data$condition == 5 | data$condition == 6]="NoMovement"
data$Movement[data$condition == 11 | data$condition == 12 | data$condition == 13 | data$condition == 14 |data$condition == 15 | data$condition == 16]="Movement"

data$ExpPhase[data$condition == 3 | data$condition == 4 | data$condition == 13 | data$condition == 14]="Acq"
data$ExpPhase[data$condition == 5 | data$condition == 6 | data$condition == 15 | data$condition == 16]="Ext"

# Add the variable defining which color is rewarded based on the participant number
data$RewardedColor = ifelse(data$Subject%%2==0,"Blue","Red") # if participant number is even, blue was rewarded

# Switch the Frequency to the color
data$RecordedFrequency = ifelse(data$Frequency==10,"Blue","Red") # if the recorded frequency is 10Hz assign Blue (color flickering at 10Hz), otherwise assign Red (color flickering at 12Hz)

# Make a new condition based on the attended color and the rewarded color
data$Condition = ifelse(data$AttendedColor==data$RewardedColor, "High_Rew","Low_Rew")

# Make a new condition based on the attended color and the recorded frequency
data$Attention = ifelse(data$AttendedColor==data$RecordedFrequency, "Att","NotAtt")

# Make a new condition based the Condition and the Attention
data$RecordingAndCondition = with(data, paste0(Condition,"_",Attention))

# Select variables which we want to keep
data = subset(data, select=c("Subject","RewardedColor","ExpPhase","AttendedColor","Condition","RecordedFrequency","Attention","RecordingAndCondition","Amplitude","Movement"))

# Sort the data 
data = data[with(data, order(Subject)), ]

# Normalize the two frequencies
#Make a new variable with mean amplitude across all conditions for each participant and each frequency  !!! originally did not include ExpPhase below !!!
# data = ddply(data,.(Subject,RecordedFrequency),transform,
#                     MeanAmplitude = mean(Amplitude[ExpPhase=="Bsln"],na.rm=TRUE),
#                     SDAmplitude =   sd(Amplitude,na.rm=TRUE))

data = ddply(data,.(Subject,RecordedFrequency),transform,
             MeanAmplitude = mean(Amplitude,na.rm=TRUE),
             SDAmplitude =   sd(Amplitude,na.rm=TRUE))

#MeanAmplitude = mean(Amplitude[ExpPhase=="Baseline"],na.rm=TRUE),   [ExpPhase=="Bsln"]

# Divide amplitudes in each Subject, Frequency, and Condition by the Mean Amplitude
data$Amplitude = data$Amplitude/data$MeanAmplitude

# Calculate the attention indexes - Selectivity (attended-unattended) & total enhancement (attended+unattended) (Andersen & Muller, 2010, PNAS)
data.diff = ddply(data, .(Subject,ExpPhase,Condition), transform, Selectivity = Amplitude[Attention=="Att"]-Amplitude[Attention=="NotAtt"],TotalEnhancement=Amplitude[Attention=="Att"]+Amplitude[Attention=="NotAtt"])
# Delete the Attention column and rows which are not necessary (indexes repeated twice)
data.diff = subset(data.diff,Attention=="Att") #keep only Att as it is equal to NotAtt
data.diff$Attention = NULL  #drop the Attention column

# Sort the data 
data.diff$ExpPhase = factor(data.diff$ExpPhase, levels = c("Bsln","Acq","Ext"))
data.diff = data.diff[order(data.diff$Subject,data.diff$Condition,data.diff$ExpPhase),]

# Take only the no movement trials
data = subset(data,Movement=="Movement")

Means - raw data

summary = ddply(data,.(Attention,ExpPhase,Condition),plyr::summarize,Mean=c(paste(round(mean(Amplitude, na.rm = TRUE), digits = 2), " [", round(hdi(Amplitude)[[1]], digits = 2), " ", round(hdi(Amplitude)[[2]], digits = 2), "]")))

names(summary) = c("Attention", "Reward phase", "Reward probability", "Amplitude")

summary$Attention = dplyr::recode(summary$Attention,
                           "Att" = "Attended",
                           "NotAtt" = "Unattended")

summary$`Reward phase` = dplyr::recode(summary$`Reward phase`,
                           "Acq" = "Acquisition",
                           "Bsln" = "Baseline",
                           "Ext" = "Extinction")

summary$`Reward probability` = dplyr::recode(summary$`Reward probability`,
                           "High_Rew" = "High",
                           "Low_Rew" = "Low")

summary = as.data.frame(summary)

summary$`Reward phase` = factor(summary$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
summary = summary[order(summary$Attention,summary$`Reward phase`,summary$`Reward probability`),]
row.names(summary) = NULL

knitr::kable(summary, caption = "Amplitudes per condition")
Amplitudes per condition
Attention Reward phase Reward probability Amplitude
Attended Baseline High 1.05 [ 0.38 1.75 ]
Attended Baseline Low 1.05 [ 0.37 1.74 ]
Attended Acquisition High 1.07 [ 0.36 1.77 ]
Attended Acquisition Low 1.04 [ 0.37 1.76 ]
Attended Extinction High 1.08 [ 0.31 1.79 ]
Attended Extinction Low 1.07 [ 0.33 1.78 ]
Unattended Baseline High 0.91 [ 0.28 1.56 ]
Unattended Baseline Low 0.91 [ 0.31 1.56 ]
Unattended Acquisition High 0.9 [ 0.3 1.57 ]
Unattended Acquisition Low 0.89 [ 0.27 1.52 ]
Unattended Extinction High 0.92 [ 0.28 1.58 ]
Unattended Extinction Low 0.94 [ 0.27 1.66 ]

Plots - raw data

# Plot amplitude across experiment phases

# prepare data for plotting
dataPlot = data

dataPlot =  ddply(dataPlot,.(Subject,ExpPhase,Condition,Attention),summarise,
                    Amplitude=mean(Amplitude,na.rm=TRUE)) # mean RTs


# rename variables
colnames(dataPlot)[colnames(dataPlot)=="ExpPhase"] <- "Reward phase"
colnames(dataPlot)[colnames(dataPlot)=="Condition"] <- "Reward probability"

# rename conditions
dataPlot$`Reward phase` = dplyr::recode(dataPlot$`Reward phase`,
                                  "Acq" = "Acquisition",
                                  "Bsln" = "Baseline",
                                  "Ext" = "Extinction")

dataPlot$`Reward probability` = dplyr::recode(dataPlot$`Reward probability`,
                                        "High_Rew" = "High",
                                        "Low_Rew" = "Low")

#order
dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(dataPlot,Attention=="Att")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(dataPlot,Attention=="NotAtt")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward phase` + `Reward probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.5,1.7), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             point.col="black", # points: color
             point.o=.3, # points: opacity (0-1)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             bty="l", # plot box type
             back.col="white") # background, color
}

Statistics - movement

# Set the working directory in order to load the models
setwd(here("brms_models"))
model.full.threefactors = readRDS("full.EEG.allsubs.movement_old_normalization.rds")

Checking the best model

Plotting the chains

# Plot chains
plot(model.full.threefactors, pars = "^b_", ask = FALSE, N=6)

Posterior predictive check

# Summary of the best model
pp_check(model.full.threefactors)

Plotting the best model

post = posterior_samples(model.full.threefactors, "^b")

# Calculate posteriors for each condition

################################################ Baseline ####

##################### Attended

######### High reward
Baseline_High_Attended = post[["b_Intercept"]]
######### Low reward
Baseline_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ConditionLow_Rew"]] 

##################### Not Attended

######### High reward
Baseline_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]]
######### Low reward
Baseline_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:AttentionNotAtt"]]

################################################ Acquistion

##################### Attended

######### High reward
Acquisition_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] 
######### Low reward
Acquisition_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]]

##################### Not Attended

######### High reward
Acquisition_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseAcq:AttentionNotAtt"]]
  
######### Low reward
Acquisition_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseAcq"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseAcq:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseAcq"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseAcq:AttentionNotAtt"]]

################################################ Extinction

##################### Attended

######### High reward
Extinction_High_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] 
######### Low reward
Extinction_Low_Attended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt"]]

##################### Not Attended

######### High reward
Extinction_High_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] +
  post[["b_ExpPhaseExt:AttentionNotAtt"]]
######### Low reward
Extinction_Low_NotAttended = post[["b_Intercept"]] + 
  post[["b_ExpPhaseExt"]] + 
  post[["b_AttentionNotAtt"]] + 
  post[["b_ConditionLow_Rew"]] + 
  post[["b_ExpPhaseExt:AttentionNotAtt"]] +
  post[["b_ConditionLow_Rew:ExpPhaseExt"]] + 
  post[["b_ConditionLow_Rew:ExpPhaseExt:AttentionNotAtt"]]
# make a data frame

posterior_conditions = melt(data.frame(Baseline_High_Attended, Baseline_High_NotAttended, Baseline_Low_Attended, Baseline_Low_NotAttended, Acquisition_High_Attended, Acquisition_High_NotAttended, Acquisition_Low_Attended, Acquisition_Low_NotAttended, Extinction_High_Attended, Extinction_High_NotAttended, Extinction_Low_Attended, Extinction_Low_NotAttended))

posterior_conditions =  posterior_conditions %>% separate(variable, c("Reward Phase", "Reward Probability", "Attention"), "_", extra = "merge")

posterior_conditions$Attention = recode(posterior_conditions$Attention,
                           "Attended" = "Attended",
                           "NotAttended" = "Unattended")

names(posterior_conditions)[4] = "Amplitude"


#order
#dataPlot$`Reward phase` = factor(dataPlot$`Reward phase`, levels = c("Baseline","Acquisition","Extinction"))
#dataPlot = dataPlot[order(dataPlot$Attention,dataPlot$`Reward phase`,dataPlot$`Reward probability`),]


plottingConditions = c("Attended","Unattended" )
for (i in 1:length(plottingConditions)){
  
  if(plottingConditions[i]=="Attended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Attended")}
  
  if(plottingConditions[i]=="Unattended"){dataAmplitudePlot=subset(posterior_conditions,Attention=="Unattended")}  

# Pirate plot

    pirateplot(formula = Amplitude ~ `Reward Phase` + `Reward Probability`, # dependent~independent variables
             data=dataAmplitudePlot, # data frame
             main=plottingConditions[i], # main title
             ylim=c(0.7,1.3), # y-axis: limits
             ylab=expression(paste("Amplitude (a.u.)")), # y-axis: label
             theme=0, # preset theme (0: use your own)
             avg.line.col="black", # average line: color
             avg.line.lwd=2, # average line: line width
             avg.line.o=1, # average line: opacity (0-1)
             bean.b.col="black", # bean border, color
             bean.lwd=0.6, # bean border, line width
             bean.lty=1, # bean border, line type (1: solid; 2:dashed; 3: dotted; ...)
             bean.b.o=0.3, # bean border, opacity (0-1)
             bean.f.col="gray", # bean filling, color
             bean.f.o=.1, # bean filling, opacity (0-1)
             cap.beans=FALSE, # max and min values of bean densities are capped at the limits found in the data
             gl.col="gray", # gridlines: color
             gl.lty=2, # gridlines: line type (1: solid; 2:dashed; 3: dotted; ...)
             cex.lab=1, # axis labels: size
             cex.axis=1, # axis numbers: size
             cex.names = 1,
             sortx = "sequential",
             bty="l", # plot box type
             back.col="white") # background, color
}

Table of means across conditions

# Make a table with conditions
posterior_means = as.data.frame(c("Attended Baseline High Reward", 
                                  "Attended Baseline Low Reward", 
                                  "Attended Acquisition High Reward", 
                                  "Attended Acquisition Low Reward", 
                                  "Attended Extinction High Reward", 
                                  "Attended Extinction Low Reward",
                                  "Unattended Baseline High Reward", 
                                  "Unattended Baseline Low Reward", 
                                  "Unattended Acquisition High Reward", 
                                  "Unattended Acquisition Low Reward", 
                                  "Unattended Extinction High Reward", 
                                  "Unattended Extinction Low Reward"))
names(posterior_means)[1] = "Condition"

posterior_means$Mean = c(paste(round(mean(Baseline_High_Attended), digits = 2), " [", round(hdi(Baseline_High_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_Attended), digits = 2), " [", round(hdi(Baseline_Low_Attended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_Attended)[[2]], digits = 2), "]"),
                        
                        paste(round(mean(Acquisition_High_Attended), digits = 2), " [", round(hdi(Acquisition_High_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_Attended), digits = 2), " [", round(hdi(Acquisition_Low_Attended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_Attended), digits = 2), " [", round(hdi(Extinction_High_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_High_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_Attended), digits = 2), " [", round(hdi(Extinction_Low_Attended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_Attended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_High_NotAttended), digits = 2), " [", round(hdi(Baseline_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Baseline_Low_NotAttended), digits = 2), " [", round(hdi(Baseline_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Baseline_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_High_NotAttended), digits = 2), " [", round(hdi(Acquisition_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Acquisition_Low_NotAttended), digits = 2), " [", round(hdi(Acquisition_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Acquisition_Low_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_High_NotAttended), digits = 2), " [", round(hdi(Extinction_High_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_High_NotAttended)[[2]], digits = 2), "]"),
                        paste(round(mean(Extinction_Low_NotAttended), digits = 2), " [", round(hdi(Extinction_Low_NotAttended)[[1]], digits = 2), " ", round(hdi(Extinction_Low_NotAttended)[[2]], digits = 2), "]"))

names(posterior_means)[2] = "Mean [HDI]"

posterior_means =  posterior_means %>% separate(Condition, c("Attention", "Reward Phase", "Reward Probability"), " ", extra = "merge")

kable(posterior_means, caption = "Means per condition")
Means per condition
Attention Reward Phase Reward Probability Mean [HDI]
Attended Baseline High Reward 1.05 [ 1.03 1.08 ]
Attended Baseline Low Reward 1.05 [ 1.02 1.08 ]
Attended Acquisition High Reward 1.07 [ 1.04 1.1 ]
Attended Acquisition Low Reward 1.04 [ 1.01 1.08 ]
Attended Extinction High Reward 1.08 [ 1.05 1.11 ]
Attended Extinction Low Reward 1.06 [ 1.04 1.09 ]
Unattended Baseline High Reward 0.91 [ 0.89 0.94 ]
Unattended Baseline Low Reward 0.91 [ 0.89 0.94 ]
Unattended Acquisition High Reward 0.9 [ 0.87 0.93 ]
Unattended Acquisition Low Reward 0.89 [ 0.85 0.92 ]
Unattended Extinction High Reward 0.92 [ 0.9 0.95 ]
Unattended Extinction Low Reward 0.94 [ 0.9 0.98 ]

Inference about the best model

Attended vs. unattended

Check the difference between attended and not attended in baseline high rewarded

Diff_Att_NotAtt_Bsln_High = Baseline_High_Attended - Baseline_High_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in baseline low rewarded

Diff_Att_NotAtt_Bsln_Low = Baseline_Low_Attended - Baseline_Low_NotAttended
plotPost(Diff_Att_NotAtt_Bsln_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition high rewarded

Diff_Att_NotAtt_Acq_High = Acquisition_High_Attended - Acquisition_High_NotAttended
plotPost(Diff_Att_NotAtt_Acq_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in acquisition low rewarded

Diff_Att_NotAtt_Acq_Low = Acquisition_Low_Attended - Acquisition_Low_NotAttended
plotPost(Diff_Att_NotAtt_Acq_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction high rewarded

Diff_Att_NotAtt_Ext_High = Extinction_High_Attended - Extinction_High_NotAttended
plotPost(Diff_Att_NotAtt_Ext_High, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between attended and not attended in extinction low rewarded

Diff_Att_NotAtt_Ext_Low = Extinction_Low_Attended - Extinction_Low_NotAttended
plotPost(Diff_Att_NotAtt_Ext_Low, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Comparison between phases

Check the difference between baseline and acquisition in high reward attended

Diff_Bsln_Acq_High_Att = Baseline_High_Attended - Acquisition_High_Attended
plotPost(Diff_Bsln_Acq_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward attended

Diff_Bsln_Acq_Low_Att = Baseline_Low_Attended - Acquisition_Low_Attended
plotPost(Diff_Bsln_Acq_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward not attended

Diff_Bsln_Acq_High_NotAtt = Baseline_High_NotAttended - Acquisition_High_NotAttended
plotPost(Diff_Bsln_Acq_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in low reward not attended

Diff_Bsln_Acq_Low_NotAtt = Acquisition_Low_NotAttended - Baseline_Low_NotAttended  
plotPost(Diff_Bsln_Acq_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward attended

Diff_Acq_Ext_High_Att = Acquisition_High_Attended - Extinction_High_Attended
plotPost(Diff_Acq_Ext_High_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward attended

Diff_Acq_Ext_Low_Att = Extinction_Low_Attended - Acquisition_Low_Attended 
plotPost(Diff_Acq_Ext_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in high reward not attended

Diff_Acq_Ext_High_NotAtt = Extinction_High_NotAttended - Acquisition_High_NotAttended 
plotPost(Diff_Acq_Ext_High_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between acquisition and extinction in low reward not attended

Diff_Acq_Ext_Low_NotAtt = Extinction_Low_NotAttended - Acquisition_Low_NotAttended 
plotPost(Diff_Acq_Ext_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Baseline difference

Check the difference between high and low reward in baseline attended

Diff_Bsln_High_Low_Att = Baseline_High_Attended - Baseline_Low_Attended
plotPost(Diff_Bsln_High_Low_Att, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between high and low reward in baseline not attended

Diff_Bsln_High_Low_NotAtt = Baseline_High_NotAttended - Baseline_Low_NotAttended
plotPost(Diff_Bsln_High_Low_NotAtt, xlab = "", col = "#b3cde0", cex = 1, showCurve = FALSE, compVal = 0)

Check the difference between baseline and acquisition in high reward unattended vs. attended

Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended = Diff_Bsln_High_Low_NotAtt - Diff_Bsln_High_Low_Att 
plotPost(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended, xlab = "", col = "#b3cde0", showCurve = FALSE, cex = 1, compVal = 0)

paste("Mean = ",round(mean(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended), digits = 2), " [", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[1]], digits = 2), " ", round(hdi(Diff_Bsln_Acq_High_vs_Low_in_attended_vs_unattended)[[2]], digits = 2), "]")
## [1] "Mean =  0  [ -0.04   0.03 ]"